Objective-C 可以打开 NSString 吗?

Can Objective-C switch on NSString?(Objective-C 可以打开 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 吗?

基础教程推荐