Created
May 28, 2015 10:55
-
-
Save lbrndnr/19fe28bc0f85311fc489 to your computer and use it in GitHub Desktop.
Swift confusing generic types
This file contains hidden or 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
class Cell: UITableViewCell { | |
func hello() -> String { | |
return "hello" | |
} | |
} | |
var payload = [Int: AnyObject]() | |
payload[1] = Cell() | |
payload[2] = UITableViewCell() | |
func cellForKey<T: UITableViewCell>(key: Int) -> T? { | |
return payload[key] as? T | |
} | |
let cell1: Cell? = cellForKey(1) // Is of type Cell, works fine | |
cell1?.hello() | |
let cell2: Cell? = cellForKey(2) // Is of type UITableViewCell instead of .None | |
cell2?.hello() // crashes |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment