Skip to content

Instantly share code, notes, and snippets.

@lukaskubanek
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukaskubanek/ed82cf679c36b5a0b0a1 to your computer and use it in GitHub Desktop.
Save lukaskubanek/ed82cf679c36b5a0b0a1 to your computer and use it in GitHub Desktop.
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