Skip to content

Instantly share code, notes, and snippets.

@zummenix
Created October 27, 2015 11:00
Show Gist options
  • Save zummenix/f10178bf696e96439221 to your computer and use it in GitHub Desktop.
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 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