This is intended behavior. For any NS_ENUMs bridged to Swift the constructor will never return nil.
Try it with some other enums in the iOS SDK bridged to Swift with unexpected values. They will all return non-nil, even for a rawValue that is not defined by the enum:
UITableViewCellStyle(rawValue: 7) // "Optional(__C.UITableViewCellStyle)"
UITableViewCellAccessoryType(rawValue: 9999) // "Optional(__C.UITableViewCellAccessoryType)"
or, with unsafeBitCast:
unsafeBitCast(42, UITableViewCellEditingStyle.self) // "Optional(__C.UITableViewCellStyle)"
Martin R pointed out that this is documented in
the Xcode 6.3 release notes:
Imported NS_ENUM types with undocumented values, such as
UIViewAnimationCurve, can now be converted from their raw integer
values using the init(rawValue:) initializer without being reset to
nil. Code that used unsafeBitCast as a workaround for this issue can
be written to use the raw value initializer. For example:
let animationCurve =
unsafeBitCast(userInfo[UIKeyboardAnimationCurveUserInfoKey].integerValue,
UIViewAnimationCurve.self)
can now be written instead as:
let animationCurve = UIViewAnimationCurve(rawValue:
userInfo[UIKeyboardAnimationCurveUserInfoKey].integerValue)!