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;
1 个解决方案
#1
80
You need to give the enum a type and then set values, in the example below North
is set as 100
, the rest will be 101
, 102
etc, just like in C
and Objective-C
.
您需要给enum一个类型,然后设置值,在下面的示例中,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
.
更新:用rawValue替换toRaw()。
#1
80
You need to give the enum a type and then set values, in the example below North
is set as 100
, the rest will be 101
, 102
etc, just like in C
and Objective-C
.
您需要给enum一个类型,然后设置值,在下面的示例中,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
.
更新:用rawValue替换toRaw()。