I’m trying to log an enum:
enum CKAccountStatus : Int {
case CouldNotDetermine
case Available
case Restricted
case NoAccount
}
NSLog("%i", CKAccountStatus.Available)
The compiler complains:
Type 'CKAccountStatus' does not conform to protocol 'CVarArg'
Why? I have tried to cast the value:
NSLog("%i", CKAccountStatus.Available as Int)
But that doesn’t fly either:
Cannot convert the expression's type '()' to type 'String'
Get the enum's underlying Int
value:CKAccountStatus.Available.rawValue
.
Enums are not strictly integers in Swift, but if they're declared with an underlying type you can get it with rawValue
— whatever that underlying type is. (enum Foo: String
will give you strings for the rawValue
, etc.) If an enum doesn't have an underlying type, rawValue
has nothing to give you. In APIs imported from ObjC, any enum defined with NS_ENUM
has an underlying integer type (typically Int
).
If you'd like to print any enum more descriptively, you might consider making an extension on the enum type that adopts the Printable
protocol.