Search code examples
swiftprotocols

Swift Protocol: property that is subclass of: someClass, instead of being of that class


I'm trying to create a protocol that describes a property that is a subclass of a certain class instead of being of that class.

I would like to know if something like this is possible.

protocol {
   var prop : T where T: SomeClass { get set} // something like this
}

versus

protocol {
  var prop : SomeClass {get set}
}

Solution

  • Use associatedtype in your protocol definition

    protocol pp {
        associatedtype T where T: SomeClass
        var prop : T  { get set} 
    }