I have a few questions about properties declared in protocols.
Return type variance
@protocol IHaveProperties
@required
@property (nonatomic, strong) IAmOfTypeX *propertyOfProtocolType;
@property (nonatomic, strong) NSArray *array;
@end
@interface ClassThatHasProperties : NSObject<IHaveProperties>
@property (nonatomic, strong) ImplementationOfTypeX *propertyOfProtocolType;
@property (nonatomic, strong) NSMutableArray *array;
@end
Okay, so I tried this with a protocol/class combination and to my chagrin, it compiled.
How does that work? Wouldn't this technically not conform to the interface?
Property declaration modifiers
@protocol IHaveProperty
@required
@property (nonatomic, strong, readonly) *example;
@end
@interface HaveProperty : NSObject<IHaveProperty>
@property (nonatomic, strong, readonly) *example;
@end
My Mac is restarting right now so I cannot try this out but I would think this would be okay, because the protocol declaration has nothing backing it. All the modifiers wouldn't be of interest to a caller, only to the class implementing the protocol.
If you look at the
<NSObject>Protocol, you can see that it holds methods and properties together:You must have used
descriptionorsuperclassproperties. There's no difference in how a property is treated, as compared to a function. The class implementing the protocol would be providing the getter and / or setter of the property of a protocol. And similarly the class which owns the delegate object would call its properties like:self.delegate.propertyName.