The difference between copy and strong (or retain)

Source: Internet
Author: User

Http://stackoverflow.com/questions/18526909/whether-i-should-use-propertynonatomic-copy-or-propertynonatomic-strong-fo

The ' copy ' would cause the setter for this property to create a copy of the object, and was otherwise identical to strong. You would use the sure if someone sets your property to a mutable string, then mutates the string, and you still Have the original value. If the string isn ' t mutable, Cocoa'll silently optimize out the copy operation, which are nice:)

' Strong ' would keep the property ' s value alive until it's set to something else. If you want incoming mutable strings-to-change out from under you (not impossible, but not all that common, a thi ng to want), then strong would is the right thing to do. Generally strong is more useful for objects, represent something more complex than a simple ' value ' (i.e. not nsstring , NSNumber, Nsvalue, etc ...).

' Assign ' is the default (and indeed only) possible setting for an integer. Integers can ' t be retained or copied like objects.

For attributes whose type was an immutable value class this conforms to the Nscopying protocol, you almost always should SP Ecify copy in your @property declaration. Specifying retain is something you almost never want in such a situation. In non ARC strong would work like retain

Here's why, you want

Nsmutablestring *Somename= [nsmutablestring Stringwithstring:@ "Chris" ];person *p =  [[[person Alloc] Init] Autorelease];. Name = Somename;[somename setstring:@ "Debajit"                

The current value of the Person.name property would be different depending on whether the property is declared retain or co Py-it would be @ ' Debajit ' If the property was marked retain, but @ ' Chris ' If the property is marked copy.

Since in almost all cases your want to prevent mutating an object ' s attributes behind it back, you should mark the Propert ies representing them copy. (And if you write the setter yourself instead of using @synthesize your should remember to actually use copy instead of RET Ain in it.)

The difference between copy and strong (or retain)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.