Created
February 17, 2017 17:43
-
-
Save chadaustin/e11c2f626b18adcdc1bcdd5421ae5e18 to your computer and use it in GitHub Desktop.
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
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