Is it possible to declare variable in @protocol? Just to force programmers to add those variable in implementing class's(classes which implements this protocol) header and implementation?
Thanks
Short answer: No, it isn't possible to do that. You can enforce the availability of methods and properties at most.
You can't declare ivars in @protocol
s but you can force the conforming class to implement an accessor and a mutator, which sounds like what you're getting at. For example:
@protocol Priced
@property(assign, nonatomic) double price;
@end
@interface Carrot : NSObject <Priced> {
double price;
}
@end
@implementation Carrot
@synthesize price;
@end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With