TheFrancisOne
TheFrancisOne

Reputation: 2737

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 ?

Upvotes: 0

Views: 78

Answers (2)

DarkDust
DarkDust

Reputation: 92316

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)

Upvotes: 1

Bill Dudney
Bill Dudney

Reputation: 3358

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.

Upvotes: 2

Related Questions