假设我有一个包含五个案例的 switch 语句,但只有两个真正的方法被调用,如下所示:
switch (condition) {
case conditionOutcome1:
[self firstMethod];
break;
case conditionOutcome2:
[self secondMethod];
break;
case conditionOutcome3:
[self firstMethod];
break;
case conditionOutcome4:
[self firstMethod];
break;
case conditionOutcome5:
[self secondMethod];
break;
default:
break;
}
像这样将病例分组安全吗?
switch (condition) {
case conditionOutcome1:
case conditionOutcome3:
case conditionOutcome4:
[self firstMethod];
break;
case conditionOutcome2:
case conditionOutcome5:
[self secondMethod];
break;
default:
break;
}
它工作得很好,但我以前从未在 Objective-C 中使用过它,所以我想通过保存几行代码来确保我不会造成任何问题。
Thanks!
只是添加到其他答案,它被称为失败,你可以阅读相关内容here http://en.wikipedia.org/wiki/Switch_statement#Fallthrough.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)