4

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?

Philip007
  • 3,190
  • 7
  • 46
  • 71
  • 1
    See http://stackoverflow.com/questions/844678/how-to-handle-objective-c-protocols-that-contain-properties and http://stackoverflow.com/questions/3377869/how-to-define-and-implement-properties-in-protocol – msk Jul 31 '12 at 18:54
  • Thanks. Yet we get a better and more concise answer here by @dasblinkenlight – Philip007 Jul 31 '12 at 20:15

3 Answers3

9

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.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

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;

Xval
  • 938
  • 5
  • 17
0

A protocol is just something that's makes sure that an object implements a set of methods. If you were to use an object as a delegate of your class for example, you would want to make sure that it implemented the methods that you're going to call. That's the point of a protocol.

John Corbett
  • 1,595
  • 10
  • 19