I am having a hard time on how to understand the actual use of setting a property to variables and objects. I tried to read many tutorials and apple documentation but still i am not able to understand on why we should use a property and why we need to synthesize the property? Considering we declare
@property(nonatomic,retain) NSString *str;
why do we need to declare a property. As far as i understand, we can access string object str through out your class. But why do we need to create a property here?
Can someone please help me out?
Properties are usually used to access instance variables from outside of the class.
For example, if you have a pointer to your class you can get/set the property like this
You also have to
@synthesizethe property, usually like this:In that snippet,
_stris the instance variable that the property sets and gets.The properties are essentially just shortcuts for creating accessors (setters/getters).
For example, a setter using your property would look something like this:
and the getter would look something like this:
By creating properties, the previous two code blocks are automatically created for you.