Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

swift : Enum constant with type and value

I know, enumeration constant should be like this in 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

enum ShareButtonID : NSInteger {    ShareButtonIDFB = 100,    ShareButtonIDTwitter,    ShareButtonIDGoogleplus  }ShareButtonID; 
like image 746
Mani Avatar asked Jun 07 '14 09:06

Mani


1 Answers

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 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.

like image 54
kmikael Avatar answered Sep 21 '22 16:09

kmikael