Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS how to implement a protocol's @property

What I understand is that a protocol specify method names, and someone else who conforms to that protocol do the implementation of the methods.

So what about the properties declared in a protocol? Is that to implement a property means to implement its setter and getter?

like image 984
Philip007 Avatar asked Dec 26 '22 20:12

Philip007


2 Answers

Property is a fancy name for one or two methods with specific signatures for which Objective-C provides a convention that lets you call them using the alternative dot . syntax. There is no difference between a protocol declaring, say, a pair of

-(int) foo;
-(void)setFoo:(int)_foo;

methods, and a protocol declaring a read-write property:

 @property (readwrite) foo;

So you are absolutely right, implementing a property means implementing one or two methods, depending on whether you implement a read-only, write-only, or a read-write property.

like image 81
Sergey Kalinichenko Avatar answered Jan 17 '23 01:01

Sergey Kalinichenko


As the others said, you just have to implements the getter and or setter (depending on the property).

I would add that you can just synthesize them :

@property (nonatomic, retain) NSObject * foo;

would end up in :

@synthesize foo;

like image 34
Xval Avatar answered Jan 17 '23 00:01

Xval