Skip to content

Instantly share code, notes, and snippets.

@chadaustin
Created February 17, 2017 17:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chadaustin/e11c2f626b18adcdc1bcdd5421ae5e18 to your computer and use it in GitHub Desktop.
Save chadaustin/e11c2f626b18adcdc1bcdd5421ae5e18 to your computer and use it in GitHub Desktop.
import Foundation
enum Enum: Int {
case one = 1
case two = 2
}
func enumFromAny<T>(_ ctor: (Int) -> T?, _ v: Any) -> T? {
if let n = v as? NSNumber {
return ctor(n.intValue)
} else {
return nil
}
}
func main() {
let n = NSNumber(integerLiteral: 2)
let a = n as Any
if let e = enumFromAny(Enum.init, a) {
print(e)
} else {
print("nil")
}
}
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment