Created
March 4, 2015 18:29
-
-
Save tomlokhorst/dd6cc06288881f27d8bd to your computer and use it in GitHub Desktop.
Invalid cast causing runtime crash in Swift <= 1.2
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 | |
let obj = NSNumber(int: 2) | |
func correct<T>() -> T? { | |
if let val = obj as? T { | |
println("matching types") | |
return val | |
} | |
else { | |
println("not matching") | |
} | |
return nil | |
} | |
func incorrect<T : NSObject>() -> T? { | |
if let val = obj as? T { | |
println("matching types") | |
return val | |
} | |
else { | |
println("not matching") | |
} | |
return nil | |
} | |
correct() as NSString? // prints "not matching" | |
incorrect() as NSString? // prints "matching types" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment