2012-07-25 55 views
2

我有开关。其中一个案件必须是阵列的计数:如何将[数组数]粘贴到大小写:

int count = [array count]; 
    switch (someValue) { 
     case 0: 
      [self foo]; 
      break; 
     case count: 
      [self bar]; 
      break; 

     default: 
      break; 
    } 

但是编译器tolds:

Expression is not an integer constant expression 

如何使const int的从[数组数]?

+0

不可能。开关不能这样工作,你必须创建自己的开关式控制结构(如果你愿意,我可以这样做)。 – 2012-07-25 15:39:14

+0

不需要,谢谢。 – Moonkid 2012-07-25 15:50:54

回答

2

由于错误提示,这些情况都必须是常量。您需要使用if声明来检查动态情况:

int count = [array count]; 
    switch (someValue) { 
     case 0: 
      [self foo]; 
      break; 
     default: 
      if (someValue == count) 
       [self bar]; 
      break; 
    } 
0
if(some value == 0) { 
    [self foo]; 
} else if (someValue == [array count]) { 
    [self bar] 
}