NSString when to use copy, when to use strong

Source: Internet
Author: User

Most of the time NSString properties are copy, then copy and strong in the case of what is the difference?

Like what:

@property (retain,nonatomic) NSString *rstr;

@property (copy, nonatomic) NSString *cstr;

-(void) test:

{

nsmutablestring *mstr = [nsmutablestringstringwithformat:@ "abc"];

Self.rstr = MSTR;

Self.cstr = MSTR;

NSLog (@ "mstr:%p,%p", mstr,&mstr);

NSLog (@ "retainstr:%p,%p", _rstr, &_RSTR);

NSLog (@ "copystr:%p,%p", _cstr, &_CSTR);

If the address of the MSTR object is 0x11, that is, 0x11 is the first address of the @ "abc", the MSTR variable itself has an address of 0x123 in memory;

When assigning MSTR to retain Rstr, the Rstr object's address is the 0X11,RSTR variable itself in memory address 0x124;rstr and MSTR point to the same address, they point to the same object @ "ABC", the object's address is 0x11, So their values are the same.

When assigning MSTR to copy's CStr, the CStr object's address is 0X22,CSTR variable itself in memory address 0x125;cstr and MSTR point to the address is not the same, they point to a different object, so copy is a deep copy, a new object, The address of this object is 0x22 and the value is @ "abc".

If you change the value of MSTR now:

[Mstr appendstring:@ "de"];

NSLog (@ "retainstr:%@", _rstr);

NSLog (@ "copystr:%@", _cstr);

Results

The value of the string rstr using retain: @ "ABCDE",

And the value of the string CStr using copy: @ "abc",

So, in general, we don't want string values to follow mstr, so we generally use copy to set the string's properties.

If you want the value of the string to follow the value of the assigned string, you can use Strong,retain.

Note: The above situation is for when the nsmutablestring assignment to NSString, the time will be different, if the assignment is NSString object, then use Copy or strong, the result is the same, Because the NSString object simply cannot change its value, he is immutable.

Assign an object to an attribute variable, and when the object changes, use the strong property if you want the property variable to change, or use the Copy property if you want the property variable to change.

From this we can see:

The source is a string of nsmutablestring, retain is merely a pointer reference, adding a reference counter, so that when the source changes, the variable declared in this retain way (whether the assigned variable is variable or immutable), it will follow the change; and copy declares the variable, it does not follow the source change, it is actually a deep copy.

The source is a string of nsstring, whether it is a variable declared by the retain or copy declaration of the variable, when the second source of the string again points to another place, it still points to the original position, that is, both are pointer references, that is, a shallow copy.

Also, the effect of both on memory counts is the same, which increases the memory reference count, which needs to be handled at the end of the day.

In fact, why should the string be used in these two ways? I think it is still a security issue, such as the declaration of a NSString *str variable, and then the assignment of a nsmutablestring *mstr variable to it, if you want STR to follow the MSTR change, then use retain; If Str cannot change with MSTR, then copy is used. And for the NSString type of string to be assigned to STR, those two are no different. Does not affect security, as is memory management.

NSString When to use copy, when to use strong

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.