开发者

Protocol method self-referenced

In my iOS project, I create a protocol named "XMLElement" and I have a problem on a method of this protocol which must return an instance of my protocol :

@protocol XMLElement <NSObject>
-(XMLElement *) GetParent;
@end

The method GetParent returns the parent element which is XMLElement protocol implementation.

But this does not compile !

Must I have to return id object ? No other way开发者_如何学Python ?


XMLElement is a protocol so you need

@protocol XMLElement <NSObject>
-(id <XMLElement>) GetParent;
@end

Not related to the problem: it is atypical to have a method name begin with an uppercase letter. It should be getParent rather than GetParent.


A protocol is no type of its own, so you need to write:

- (id<XMLElement>) getParent;

(I recommend you stick to the conventions and start methods with lowercase letters)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜