我知道,枚举常量在swift中应该是这样的
enum CompassPoint {
case North
case South
case East
case West
}
但是如何给第一个元素赋值,如下面的Objective-C代码
enum ShareButtonID : NSInteger
{
ShareButtonIDFB = 100,
ShareButtonIDTwitter,
ShareButtonIDGoogleplus
}ShareButtonID;
最佳答案
你需要给枚举一个类型然后设置值,在下面的例子中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.
更新:将toRaw()
替换为rawValue
。
关于ios - swift : Enum constant with type and value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24095741/