What copy attribute does in objective c [duplicate] - objective-c

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

Related

iOS7 is there a reason to use assign instead of weak for properties? [duplicate]

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.

What is difference between strong and copy? [duplicate]

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.

What of the mean of underline in objective-C [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How does an underscore in front of a variable in a cocoa objective-c class work?
Why rename synthesized properties in iOS with leading underscores?
I am a green hand in iOS programming.
I always see such a statement in other's code
#synthesize textNoteOrLink = _textNoteOrLink;
What is the meaning of the underline anyway? Can we just 'textNoteOrLink' in that case.
Yes you can just write textNoteOrLink.
Many developers put an underscore at the start of instance variable names (#synthesizing a property actually adds an ivar for that property) to avoid accidentally using the ivars instead of the property, bypassing setters and getters.
IMHO it's a good thing to do, but if you don't like it, just don't use it, but be cautions not to confuse properties and ivars.

Property and synthesize in iOS [duplicate]

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.

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