可以在Objective-C上打开NSString吗? [英] Can Objective-C switch on NSString?
本文介绍了可以在Objective-C上打开NSString吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有更智能的方式来重写它?
Is there a more intelligent way to rewrite this?
if ([cardName isEqualToString:@"Six"]) {
[self setValue:6];
} else if ([cardName isEqualToString:@"Seven"]) {
[self setValue:7];
} else if ([cardName isEqualToString:@"Eight"]) {
[self setValue:8];
} else if ([cardName isEqualToString:@"Nine"]) {
[self setValue:9];
}
推荐答案
不幸的是,他们不能.这是使用switch语句的最佳方式和最受欢迎的方法之一,因此希望他们跳上(现在)Java(和其他)潮流!
Unfortunately they cannot. This is one of the best and most sought after utilizations of switch statements, so hopefully they hop on the (now) Java (and others) bandwagon!
如果要输入卡名,则可以为每个卡对象分配一个整数值,然后将其打开.也许是一个枚举,它被认为是一个数字,因此可以打开.
If you are doing card names, perhaps assign each card object an integer value and switch on that. Or perhaps an enum, which is considered as a number and can therefore be switched upon.
例如
typedef enum{
Ace, Two, Three, Four, Five ... Jack, Queen, King
} CardType;
这样,Ace等于情况0,两个等于情况1,依此类推.
Done this way, Ace would be be equal to case 0, Two as case 1, etc.
这篇关于可以在Objective-C上打开NSString吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文