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残りはなどになります。101102CObjective-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 に答える