【发布时间】:2012-01-12 04:20:54
【问题描述】:
我一直想知道但找不到明确的答案。我什么时候应该使用“if”或“switch”语句。什么时候比另一个更好?
例如我可以这样做:
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
switch (buttonIndex) {
case 0:
//Stuff
break;
case 1:
//Stuff
break;
default:
break;
}
//OR
if (buttonIndex == 0) {
//Stuff
}
if (buttonIndex == 1) {
//Stuff
}
}
这只是一个例子,但我相信在不同的情况下它会有所不同。
【问题讨论】:
标签: iphone objective-c if-statement switch-statement