错误maddage:预期的表达?

时间:2014-03-03 04:28:29

标签: objective-c compiler-errors switch-statement

我认为这可能是一个愚蠢的错误,但我无法弄明白。任何人都可以告诉我,我错过了什么?

-(void) receiveRecommendData {
 //receive data//
 NSURL *getResultUrl = [NSURL URLWithString:@"http://phdprototype.tk/getResultData.php"];

 NSData *data = [NSData dataWithContentsOfURL:getResultUrl];

 [self getData:data];

 NSDictionary *jsonToDict = json[0]; //[json objectAtIndex:0];

 int pageNumber = [[jsonToDict objectForKey:@"recommendData"]intValue];

 switch (pageNumber) {
     case 0:
         UIViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"Collection_01ViewController"];
         break;
     case 1:
         UIViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"Collection_01ViewController"];
         break;
     case 2:
          UIViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"Collection_02ViewController"];
          break;
     case 3:
          UIViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"Collection_03ViewController"];
         break;
     default:
         break;
 }
}

错误发生在

的代码上
UIViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"Collection_01ViewController"]; 

1 个答案:

答案 0 :(得分:4)

您不能将变量声明为案例部分的第一行。只需添加大括号......

switch (pageNumber) {
  case 0: {
     UIViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"Collection_01ViewController"];
     break;
  }
  case 1: {
     UIViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"Collection_01ViewController"];
     break;
  }
  case 2:  //etc