Switch-Case 语句和数字范围 [英] Switch-Case Statement and Range of Numbers
本文介绍了Switch-Case 语句和数字范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法在 Objective C(在 XCode 中)中使用带有范围的 switch 语句,假设是这样的:
Is there a way to use switch statement with ranges in Objective C (in XCode), hypothetically something like this:
- (NSString *)evaluate:(NSInteger)sampleSize
{
NSString returnStr;
switch (sampleSize)
{
case sampleSize < 10:
returnStr = @"too small!";
break;
case sampleSize >11 && sampleSize <50:
returnStr = @"appropriate";
break;
case sampleSize >50:
returnStr = @"too big!";
break;
}
return returnStr;
}
推荐答案
有一个 GCC 扩展(我假设 Clang 支持它)可能适合您.它允许您在 case 语句中使用范围.完整文档位于 http://gcc.gnu.org/onlinedocs/gcc-4.2.4/gcc/Case-Ranges.html#Case-Ranges - 来自该页面的示例 case 语句是
There is a GCC extension (which I assume is supported in Clang) that might be suitable for you. It allows you to use ranges in case statements. The full documentation is at http://gcc.gnu.org/onlinedocs/gcc-4.2.4/gcc/Case-Ranges.html#Case-Ranges - an example case statement from that page is
case 1 ... 5:
这将匹配(不出所料)1、2、3、4 或 5.
which would match (unsurprisingly) 1, 2, 3, 4, or 5.
这篇关于Switch-Case 语句和数字范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文