开发者

Can you declare a variable type that both expects a class and protocol in objective-c?

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜