37

我知道,枚举常量应该是这样的

enum CompassPoint {
    case North
    case South
    case East
    case West
}

但是我怎样才能给第一个元素赋值,比如下面的Objective-C代码

enum ShareButtonID : NSInteger
{
   ShareButtonIDFB = 100,
   ShareButtonIDTwitter,
   ShareButtonIDGoogleplus

}ShareButtonID;
4

1 回答 1

98

您需要给枚举一个类型,然后设置值,在下面的示例中North设置为100,其余的将是101102等等,就像 inCObjective-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.

于 2014-06-07T09:32:11.950 回答