swift : Enum constant with type and value
37,658
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
.
Author by
Mani
Working as Sr. IOS Engg at MVI Technology with 9+ exp. Experience in Swift, Objective-C, Android. Worked with Rolo for Android @ Netmine Mobile Innovations. I've published Tattle-UI-IOS control. It is an testing kit used to beta-tester for reporting UI issue by marking on current screen's snapshot.(like UI misalignment, font..etc). Earned Marshal badge at No.884.
Updated on February 17, 2020Comments
-
Mani about 4 years
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;
-
nacho4d about 9 years
toRaw()
has been replaced withrawValue
-
kmikael about 9 yearsThanks for the reminder, nacho4d. I have updated the answer with a short comment about the change.
-
kmikael about 8 years@fnc12: There is not Swift 2.3…