Skip to content

Instantly share code, notes, and snippets.

@lukaskubanek
Last active August 29, 2015 14:13
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
Swift: Conformance to a base class and a protocol
class BaseClass {}
protocol Protocol {}
class Container<T where T: BaseClass, T: Protocol> {
var array: [T] = []
}
class FirstConformClass: BaseClass, Protocol {}
class SecondConformClass: BaseClass, Protocol {}
// This doesn't work because the base class itself doesn't conform to the protocol.
//let container = Container()
// Instantiating the container with a concrete conform type does work...
let container = Container<FirstConformClass>()
container.array.append(FirstConformClass())
// ...but in that case objects of type SecondConformClass cannot be stored in the array
//container.array.append(SecondConformClass())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment