Skip to content

Instantly share code, notes, and snippets.

@diegosanchezr
Last active October 28, 2015 13:46
Show Gist options
  • Save diegosanchezr/7b2e784fce8da35d22a2 to your computer and use it in GitHub Desktop.
Save diegosanchezr/7b2e784fce8da35d22a2 to your computer and use it in GitHub Desktop.
Protocol conformance in collections
import Foundation
protocol UniqueIdentificableProtocol {
var uid: String { get }
}
protocol UniqueIdentificableSubtypeProtocol: UniqueIdentificableProtocol {
var c: String { get }
}
class UniqueIdentificableClass {
var uid: String { return "uid" }
}
class UniqueIdentificableSubtypeClass: UniqueIdentificableClass {
var c: String { return "c"}
}
let myCollectionClasses: [UniqueIdentificableSubtypeClass] = [UniqueIdentificableSubtypeClass]()
let myCastedCollectionClasses: [UniqueIdentificableClass] = myCollectionClasses // Everything ok with classes
let myCollectionProtocols = [UniqueIdentificableSubtypeProtocol]()
let myCastedCollectionProtocols: [UniqueIdentificableProtocol] = myCollectionProtocols // Compiler error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment