In Objective-c if two classes adopt a particular protocol can instances of the classes be used interchangeably?
Say I have the following code:
@protocol MyProtocol
@required
@property (nonatomic, retain) SomeObject *object;
@end
@interface ClassA <MyProtocol>
@property (nonatomic, retain) SomeObject *object;
// ...
@end
@interface ClassB <MyProtocol>
@property (nonatomic, retain) SomeObject *object;
// ...
@end
Can I substitute (id <MyProtocol>)instanceOfClassB when a method expects an instance of ClassA ?
Nope. Instances of different classes that conform to the same protocol can be used interchangeably when the API is explicitly typed that way though, e.g.: