S.Captain
S.Captain

Reputation: 167

The difference between id<protocol> someObject and NSObject<protocol> *someObject

There are three kind of protocol implementation:

The first:

@protocol FirstProtocol
...
@end

@property(nonatomic, weak) id<FirstProtocol> delegate;

The second:

@protocol SecondProtocol<NSObject>
...
@end

@property(nonatomic, weak) id<FirstProtocol> delegate;

The third:

@protocol SecondProtocol
...
@end

@property(nonatomic, weak) NSObject<FirstProtocol> *delegate;

I just know the "<NSObject> or NSObject<protocol>" can let the delegate call NSObject selector. But I don't know what's the difference between them. And which one is the best Practice.

Upvotes: 1

Views: 53

Answers (1)

Jeffery Thomas
Jeffery Thomas

Reputation: 42588

You are making claims about what methods can be called on delegate.

In id<FirstProtocol>, the only supported methods are ones specified in the FirstProtocol.

In id<SecondProtocol>, the supported methods are ones specified in SecondProtocol and the NSObject protocol. This gives you access to -class, -superclass, -isEqual:, -hash, -self, and all the other methods in the NSObject protocol.

In NSObject<FirstProtocol>, the object must be a kind of NSObject. It has access to -copy, -mutableCopy, and everything else which depends the NSObject class.

Upvotes: 2

Related Questions