Skip to content

Instantly share code, notes, and snippets.

@lbrndnr
Created May 28, 2015 10:55
Show Gist options
  • Save lbrndnr/19fe28bc0f85311fc489 to your computer and use it in GitHub Desktop.
Save lbrndnr/19fe28bc0f85311fc489 to your computer and use it in GitHub Desktop.
Swift confusing generic types
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