user1467188
user1467188

Reputation: 627

iOS when should @property be in .h and when in .m

I understand that methods available for other classes to call should be in the header file..but I'm a little confused when the @property should be in the header file and when it should be in the implementation file.

How do you make that decision, and what difference does it make?

Upvotes: 3

Views: 1535

Answers (2)

Cameron Lowell Palmer
Cameron Lowell Palmer

Reputation: 22245

Any property that you want publicly exposed to the other classes goes in the .h file. The 'private' properties (pun intended) go in the implementation file in a anonymous category or class extension. You might also make the .h version of the property readonly for example, and the .m version readwrite.

An implementation file with properties defined in a Private Category

@interface CPClassFileName ()
@property (nonatomic, retain) NSString *string;
@end



@implementation
...
@end

Upvotes: 6

Marcio
Marcio

Reputation: 2107

You put the @property on the header if you want other classes (or developers) to know that there are accessors to your ivars.

To rephrase : If you want "public" getter / setter you put the @property on the header. If you want them private you put the @property on the .m file.

Upvotes: 0

Related Questions