objective-c 如何制作枚举案例的开关案例(swift)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26190828/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to make switch case of enum case (swift)
提问by KennyVB
I got this code from ObjC. I want to convert it to Swift, however, I am having difficulties doing so...
我从 ObjC 得到了这段代码。我想将它转换为 Swift,但是,我在这样做时遇到了困难......
ObjC code :
对象代码:
navgivet.h
navgivet.h
typedef NS_ENUM(NSInteger, BB3Photo) {
kirkenType = 10 ,
festenType = 20 ,
praestType = 30
};
@property (nonatomic, assign) BB3Photo selectedPhotoType;
navgivet.m
navgivet.m
- (IBAction)changeImage:(id)sender {
if ([sender isKindOfClass:[UIButton class]]) {
UIButton *button = sender;
_selectedPhotoType = button.tag;
}
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"V?lg Billed"
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:@"V?lg fra Biblioteket", @"V?lg Kamera", nil];
sheet.actionSheetStyle = UIActionSheetStyleDefault;
[sheet showInView:[self.view window]];
}
}
switch (_selectedPhotoType) {
case kirkenType: {
}break;
case festenType: {
}break;
case praestType: {
}break;
default:
break;
Here's my swift code in this attempt
这是我在这次尝试中的快速代码
enum BBPhoto1: Int {
case kommunen = 10
case sagsbehandler = 20
case festen = 30
}
@IBAction func changeImage(sender: AnyObject){
if sender .isKindOfClass(UIButton){
let button: UIButton = sender as UIButton
selectedPhoto = BBPhoto1.fromRaw(button.tag)
}
let actionSheet = UIActionSheet(title: "Billeder", delegate: self, cancelButtonTitle: nil, destructiveButtonTitle: nil, otherButtonTitles: "V?lg fra Biblioteket", "V?lg Kamera")
actionSheet.showInView(self.view)
}
var selectedPhoto: BBPhoto1?
switch (selectedPhoto) {
case kommunen {
}
case sagsbehandler{
}
}
but I get errors : "Use of unresolved identifier kommunen" and same messege but with Sagsbehandler.
但我收到错误:“使用未解析的标识符 kommunen”和相同的消息,但使用 Sagsbehandler。
How do I make it work ?
我如何使它工作?
回答by Antonio
There are 3 problems in your code.
您的代码中有 3 个问题。
The first is that selectedPhotois declared as optional, so you must unwrap it before using in a switch statement - for example using optional binding.
第一个是selectedPhoto声明为可选的,因此您必须在 switch 语句中使用之前解开它 - 例如使用可选绑定。
The second problem is that the syntax you're using is incorrect. In each caseyou have to specify the full name (including the type), followed by a colon:
第二个问题是您使用的语法不正确。在每个中,case您必须指定全名(包括类型),后跟一个冒号:
case BBPhoto1.kommunen:
// statements
but since the type can be inferred by the variable type used in the switch, you can ignore the enum type, but not the dot:
但是由于可以通过开关中使用的变量类型推断类型,因此您可以忽略枚举类型,但不能忽略点:
case .kommunen:
// statements
Last, in swift a switchstatements require that all cases are handled either explicitly (3 in your case) or using a defaultcase covering all cases non explicitly handled in the switch.
最后,在 swift 中,aswitch语句要求显式处理所有案例(在您的案例中为 3)或使用default涵盖switch.
A working version of your code would look like:
您的代码的工作版本如下所示:
enum BBPhoto1: Int {
case kommunen = 10
case sagsbehandler = 20
case festen = 30
}
var selectedPhoto: BBPhoto1?
if let selectedPhoto = selectedPhoto {
switch (selectedPhoto) {
case .kommunen:
println(selectedPhoto.toRaw())
case .sagsbehandler:
println(selectedPhoto.toRaw())
default:
println("none")
}
}
Note that, differently from other languages, the code in each case doesn't automatically fallthrough to the next case, so the breakstatement is not required - the only use case for it is when a case has no statement (a case with no statement is an error in swift), and in that case the breakjust acts as a placeholder and its meaning is 'do nothing'.
请注意,与其他语言不同,每种 case 中的代码不会自动落入下一个 case,因此break不需要该语句 - 它的唯一用例是 case 没有语句(没有语句的 case 是swift 中的错误),在这种情况下,breakjust 充当占位符,其含义是“什么都不做”。
Suggested reading: Conditional Statements
建议阅读:条件语句

