PrepareForSegue之谜

时间:2014-02-18 18:31:30

标签: ios objective-c switch-statement

我在两个不同的VC中有一个prepareForSegue方法。一个使用if语句,而另一个使用switch。代码几乎完全相同,但名称除外。

这个很好用:

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
        NSManagedObjectContext *localContext = [NSManagedObjectContext MR_contextForCurrentThread];
    if ([[segue identifier] isEqualToString:@"addActivity"])
    {
        UINavigationController *navController = (UINavigationController *)segue.destinationViewController;
        AddActivityViewController *aavc = (AddActivityViewController *)navController.topViewController;
        aavc.delegate = self;
        ListActivity *addedActivity = (ListActivity *)[ListActivity MR_createInContext:localContext];
        aavc.thisActivity = addedActivity;
    }

这个给了我两个警告。在第一行,我得到一个“预期表达式”警告。在第二行,我得到“使用未声明的标识符'NavController'。

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    NSManagedObjectContext *localContext = [NSManagedObjectContext MR_contextForCurrentThread];
    [SearchSpecs MR_truncateAllInContext:localContext];
    [localContext MR_saveToPersistentStoreAndWait];

    switch ([sender tag])
    {
        case aVsAButton_tag:
            UINavigationController *navController = (UINavigationController *)segue.destinationViewController;
            AvsAViewController *aVSaVC = (AvsAViewController *)navController.topViewController;
            aVSaVC.delegate = self;
            SearchSpecs *thisSpec = (SearchSpecs *)[SearchSpecs MR_createInContext:localContext];
            aVSaVC.currentSpec = thisSpec;

            break;

        default:
            break;
    }

}

有人可以指出我的错误吗?

谢谢!

修改

所有答案都解决了这个问题,非常感谢大家!

这是我的新代码:

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    NSManagedObjectContext *localContext = [NSManagedObjectContext MR_contextForCurrentThread];
    [SearchSpecs MR_truncateAllInContext:localContext];
    [localContext MR_saveToPersistentStoreAndWait];

    switch ([sender tag])
    {
        case aVsAButton_tag:
        {
            UINavigationController *navController = (UINavigationController *)segue.destinationViewController;
            AvsAViewController *aVSaVC = (AvsAViewController *)navController.topViewController;
            aVSaVC.delegate = self;
            SearchSpecs *thisSpec = (SearchSpecs *)[SearchSpecs MR_createInContext:localContext];
            aVSaVC.currentSpec = thisSpec;
        }
            break;

        default:
            break;
    }

}

当我按照第三个答案的建议添加分号时,我在default:行收到了“Switch case in protected scope”的警告。但是,当我将case代码括在大括号中时,所有问题都会消失。我记得非常好!

我会对所有答案进行绿色检查,但由于它们都是同时到达的,我希望如果我接受顶级答案,任何人都不会被冒犯。为所有人+1,再次感谢!

3 个答案:

答案 0 :(得分:3)

要解决第二个错误,请尝试在switch-case中添加大括号以定义变量的上下文:

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    NSManagedObjectContext *localContext = [NSManagedObjectContext MR_contextForCurrentThread];
    [SearchSpecs MR_truncateAllInContext:localContext];
    [localContext MR_saveToPersistentStoreAndWait];

    switch ([sender tag])
    {
        case aVsAButton_tag:
            {
                UINavigationController *navController = (UINavigationController *)segue.destinationViewController;
                AvsAViewController *aVSaVC = (AvsAViewController *)navController.topViewController;
                aVSaVC.delegate = self;
                SearchSpecs *thisSpec = (SearchSpecs *)[SearchSpecs MR_createInContext:localContext];
                aVSaVC.currentSpec = thisSpec;
            }
            break;

        default:
            break;
    }

}

答案 1 :(得分:3)

在C / Objective-C中,你不能像这样在switch语句中声明变量。如果要声明变量以便在switch语句的特定情况下使用,可以将该案例的所有代码放在语句块中:

switch ([sender tag])
{
    case aVsAButton_tag:
    {
        UINavigationController *navController = (UINavigationController *)segue.destinationViewController;
        AvsAViewController *aVSaVC = (AvsAViewController *)navController.topViewController;
        aVSaVC.delegate = self;
        SearchSpecs *thisSpec = (SearchSpecs *)[SearchSpecs MR_createInContext:localContext];
        aVSaVC.currentSpec = thisSpec;
    }
        break;

    default:
        break;
}

答案 2 :(得分:3)

这里有两个不同的问题。

您可以在C / Objective-C中声明变量 在switch语句中(不需要额外的{ ... }范围), 但不能立即 跟随标签 。要解决这个问题就足够了 在标签后插入分号:

switch (i) {
    case 0: ;
        int i;
        // ...
        break;

    default:
        break;
}

仅当您声明Objective-C 对象 并使用 ARC 进行编译时,您才需要 引入一个额外的范围:

switch (i) {
    case 0: {
        NSObject *obj;
        // ...
        } break;

    default:
        break;
}

原因是ARC编译器需要知道对象的精确生命周期。