Last active
August 29, 2015 14:13
-
-
Save lukaskubanek/ed82cf679c36b5a0b0a1 to your computer and use it in GitHub Desktop.
Swift: Conformance to a base class and a protocol
This file contains 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 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
Sample code for my Stack Overflow question: http://stackoverflow.com/questions/25767156/swift-property-conforming-to-a-specific-class-and-in-the-same-time-to-multiple/25771265?noredirect=1#comment44436658_25771265