【发布时间】: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