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)
精彩评论