2014-02-18 61 views
0

我在兩個不同的VC中有一個prepareForSegue方法。一個使用if聲明,而另一個使用switch。代碼幾乎完全相同,除了名稱。PrepareForSegue mystery

這一個正常工作:

- (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:線。但是,當我將大寫的case代碼放在大括號中時,所有問題都消失了。非常好的事情讓我記住!

我會綠色檢查所有答案,但因爲它們都是同時到達的,所以我希望沒有人會因爲接受第一個答案而被冒犯。爲所有人+1,並再次感謝!

+1

如果你在switch case中創建對象,你必須使用:'{}' – Larme

回答

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; 
} 
+1

[這是一個很好的解釋](http://stackoverflow.com/questions/92396/why-cant-variables-be-當你試圖做這樣的事情時,爲什麼你需要括號。 – bdesham

+0

也在C++中是這種情況 – Jeef

+0

感謝您的有用鏈接@bdesham! – rattletrap99

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

} 
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編譯器需要知道t的精確壽命他反對。