objective-c properties question
I'm new to objective-c and have been wondering what's the point of creating properties? I can just create a variable in the header? Why create a vari开发者_如何转开发able and a property with the same name?
Easier memory management (for some), better design (for all). Properties make your public interface to the class concrete, and it defines what is weakly and what is strongly referenced.
As an addition to what Joshua have said: properties are KVC/KVO-compatible while variables are not, whole Cocoa stands on KVO. You can bind a property and to property, you can add an observer, you can use valueForKey/valueForKeyPath and their 'set' brothers for free. If the value must be published (i.e. available for external classes) - make a property, you will get a lot of stuff for free. Just make sure you've set the proper memory management option (assign, copy or retain) and thread-safety modifier (nonatomic).
精彩评论