Property and synthesize in iOS [duplicate] - objective-c

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
#property #synthesize
When must I use property and synthesize for an element as NSArray, NSSTring....or IBOutelt as UIButton or UITextFiled?

A property is used mainly when other objects need to change or access the ivars in your object. Without manually defining getters and setters, or using #property, other objects can't see or change the ivars. Properties are also often used for convenience of memory management, assisting you in preventing memory leaks.

Related

Why use self if code works without it? [duplicate]

This question already has answers here:
Difference between class property mVar and instance variable self.mVar
(3 answers)
Closed 9 years ago.
Really, why most of people use self.something for almost everything when code works without it?
for example:
-(void)viewDidLoad {
self.mylabel.text = [NSString stringWithFormat:#" %#", someVariable];
}
The code works the same way without self.
I have tried to see what happens without self and it always works.
Using self. means that you are using the getter/setter, and not using it means that you are accessing the instance variable directly.
This question has been treated a lot here, but summarising:
ALWAYS create a #property for every data member and use “self.name” to
access it throughout your class implementation. NEVER access your own
instance variables directly.
Properties enforce access restrictions (such as readonly)
Properties enforce memory management policy (strong, weak)
Properties provide the opportunity to transparently implement custom
setters and getters.
Properties with custom setters or getters can be used to enforce a
thread-safety strategy. Having a single way to access instance
variables increases code readability.
Source:
Best Practices fr Obj-C

Objective C - whats the advantage or rationale of properties? [duplicate]

This question already has answers here:
Property vs. instance variable
(7 answers)
what is a member vs. a property
(9 answers)
Closed 9 years ago.
I am new to objective c
All the time i read something about properties and delegates
#synthesize something;
#property (nonatomic, retain) IBOutlet something<Something> Something;
While my program gets bigger and bigger i find myself not using this at all and everything is working just fine.
so my question: what are properties for? Whats there advantage over normal variables with getters and setters?
Properties are normal variables with getters and setters, but provide a much shorter way to write them.

What copy attribute does in objective c [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Xcode property attributes (nonatomic , copy , strong , weak)
I want to know the Copy attribute working process with any example.
Thanks
copy - "Specifies that a copy of the object should be used for assignment. ... The previous value is sent a release message." Basically same as retain, but sending -copy rather than -retain.
The retain/copy concepts come from the reference-counted memory management system.
Essentially, when used with #synthesize directive (to synthesize the property accessors you would otherwise write - like -person or -setPerson:), it determines how memory is managed inside the setter.
check out this previous asked question's answer

What is the difference of these #properties in Objective c? [duplicate]

This question already has answers here:
What's the difference between the atomic and nonatomic attributes?
(27 answers)
Closed 9 years ago.
What is the difference between atomic and nonatomic properties, and what does it have to do with retaining it?
I know what #property(retain) is, defined in this website: The #property is an Objective-C directive which declares the property. The "retain" in the parenthesis specifies that the setter should retain the input value, and the rest of the line simply specifies the type and the name of the property.
So #property(retain) does what was stated above, but how does nonatomic/atomic function with the retain property?
#property(nonatomic, retain)
#property(atomic, retain)
retain and atomic/nonatomic are orthogonal, meaning that any combination of them is valid. retain says that there is a strong link between the object and its retained property (i.e. the object referenced by the property should not be released while it is pointed to by this object). atomic/nonatomic means that the access to the property should or should not be synchronized. Here is a great explanation of the atomic/nonatomic.
Note that all of this is meaningful only when you use #synthesize.

Do Properties have to be declared as Instance Variables in Objective C? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Properties and Instance Variables in Objective-C 2.0
If I create a #property in a header file and the #synthesize it, everything seems to work fine, even if the item is not also declared as an instance variable. So why does all the example code I see declare items as both properties and instance variables?
The #property command in Objective-C 2.0 will automatically generate the instance variable for you if you have not done so. This is a shortcut introduced to limit the amount of repetitive code you have to write.
Only declare the iVar's if:
Need to directly access them for some advanced reason (i.e. you want to manage the memory yourself)
You want subclasses to be able to access the iVars (if you don't specify them at all, or specify them as #private, then subclasses will be forced to use your #synthesized accessor methods.)
You want your iVar to have a different name to the property itself, in which case use #synthesize myProperty = myInstanceVariable_
NOTE: If you plan to run your code on older devices or compile it with older versions, you will need to declare iVars.
No, you dont.
But the autogenerated methods will try to access the instance variables, so you have to implement the setter and the getter for the property you have added.
See Vladimirs comment with the following link:
Properties and Instance Variables in Objective-C 2.0