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 等于 case 0,2 等于 case 1,等等.
Done this way, Ace would be be equal to case 0, Two as case 1, etc.
这篇关于Objective-C 可以打开 NSString 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Objective-C 可以打开 NSString 吗?
- 如何在 iPhone 模拟器中重置 NSUserDefaults 数据? 2022-01-01
- URL编码Swift iOS 2022-01-01
- SetOnItemSelectedListener上的微调程序错误 2022-01-01
- 类似于 Mail.app 的 iPad 模态视图控制器? 2022-01-01
- GPS状态的广播接收器? 2022-01-01
- Xcode 7.3 中带有 UILabel 的 UIStackView 2022-01-01
- 在 Iphone SDK 的导航栏上添加多个按钮 2022-01-01
- 使用自动布局向 UIScrollView 添加动态大小的视图 2022-01-01
- UITextView 内容插图 2022-01-01
- 网上有没有好的 UIScrollView 教程? 2022-01-01