ios swift :具有类型和值的枚举常量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24095741/
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
swift : Enum constant with type and value
提问by Mani
I know, enumeration constant should be like this in swift
我知道,枚举常量在swift中应该是这样的
enum CompassPoint {
case North
case South
case East
case West
}
But how can I assign value to first element, like Objective-C code as below
但是如何为第一个元素赋值,如下面的 Objective-C 代码
enum ShareButtonID : NSInteger
{
ShareButtonIDFB = 100,
ShareButtonIDTwitter,
ShareButtonIDGoogleplus
}ShareButtonID;
回答by kmikael
You need to give the enum a type and then set values, in the example below Northis set as 100, the rest will be 101, 102etc, just like in Cand Objective-C.
你需要给枚举类型,然后设置值,在下面的例子中North被设定为100,其余的将是101,102等等,就像在C和Objective-C。
enum CompassPoint: Int {
case North = 100, South, East, West
}
let rawNorth = CompassPoint.North.rawValue // => 100
let rawSouth = CompassPoint.South.rawValue // => 101
// etc.
Update: Replace toRaw()with rawValue.
更新:替换toRaw()为rawValue.

