I know that you can do one of the following things when declaring the type of a variable in objective c:
id obj0;
MyClass *obj1;
id<MyProtocol> obj2;
What i'm curious about is if this is valid (syntactically and semantically):
MyClass<MyProtocol> *obj3;
What i wan开发者_Python百科t is to store a cocoa class that must implement a given protocol in this variable; if i had control over "MyClass" i wouldnt need this but i'm basically wondering if i can get away with not having to make my own abstract class that multiple other disparate classes need to inherit from, when they can otherwise just inherit from "MyClass" directly.
Yes.
MyClass<MyProtocol> *obj3;
means that obj3
should be a pointer to an object of type MyClass
or a subclass, that also implements MyProtocol
.
精彩评论