Created
October 27, 2015 11:00
-
-
Save zummenix/f10178bf696e96439221 to your computer and use it in GitHub Desktop.
Objectvie-C / Swift interoperability issue. Failable initializer for objc enum doesn't work as expected.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This creates UIViewAnimationCurve with invalid rawValue. | |
// Should be exeption while unwrapping optional value. | |
let curve = UIViewAnimationCurve(rawValue: 9)! | |
// Prints "ease in out" (This is a first variant in the enum). | |
switch curve { | |
case .EaseInOut: | |
print("ease in out") | |
case .EaseIn: | |
print("ease in") | |
case .EaseOut: | |
print("ease out") | |
case .Linear: | |
print("linear") | |
} | |
// Equility doesn't work in this case. | |
// assert(curve == .EaseInOut) | |
// Equility using hashValue works as expected. | |
assert(curve.hashValue == UIViewAnimationCurve.EaseInOut.hashValue) | |
// Hacky way to create objc enum variant using hashValue. | |
assert(UIViewAnimationCurve(rawValue: (UIViewAnimationCurve(rawValue: 9) ?? .EaseInOut).hashValue) == .EaseInOut) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment