【问题标题】:PrepareForSegue mysteryPrepareForSegue之谜
【发布时间】:2014-02-18 18:31:30
【问题描述】:

我在两个不同的 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 is in protected scope”的警告。但是,当我将case 代码括在大括号中时,所有问题都烟消云散了。非常值得我记住!

我会对所有答案进行绿色检查,但由于它们几乎同时到达,我希望如果我接受排名靠前的答案,没有人会被冒犯。为所有人 +1,再次感谢!

【问题讨论】:

  • 如果在 switch case 中创建对象,则必须使用:{}

标签: ios objective-c switch-statement


【解决方案1】:

在 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;
}

【讨论】:

  • Here is a good explanation 在尝试执行此类操作时为什么需要括号。
  • 在 C++ 中也是如此
  • 感谢@bdesham 提供的有用链接!
【解决方案2】:

要解决第二个错误,请尝试在 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;
    }

}

【讨论】:

    【解决方案3】:

    这里有两个不同的问题。

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

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

    只有当你声明 Objective-C objects 并使用 ARC 编译时,你必须 引入一个额外的范围:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-22
      • 2018-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-16
      相关资源
      最近更新 更多