This question already has answers here:
Objective-C declared #property attributes (nonatomic, copy, strong, weak)
(4 answers)
Closed 9 years ago.
What is the difference between strong vs copy in objective-c? Which one should i be using?
I know copy prevents the value of the instance variable from changing if set with a mutable string that is later changed itself. Anything else?
strong increases retain counter of an object by 1.
copy creates an object's copy with retain counter 1.
If you use ARC you can't access to retain counter, but the approach is the same as for MRC.
Related
This question already has answers here:
Objective-C ARC: strong vs retain and weak vs assign
(8 answers)
Closed 8 years ago.
I've encountered several "message sent to deallocated instance" bugs within my app and traced them to the use of
#property(nonatomic,assign)NSObject* object;
Replacing them with
#property(nonatomic,weak)BuffCollection* buffCollection;
solves the problem. Should I define all of my properties where I don't want to use strong to be using weak instead of assign?
Theres a great explanation of all the different property attributes here.
If you are using ARC, the basics are to use strong for obj-c objects you want to retain, weak for obj-c objects you don't want to retain and assign for non-objective-c (so C) primatives. Strong is default.
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.
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
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.
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.