When does NSString use copy and strong?

Source: Internet
Author: User

Most of the time, the NSString attribute is copy. What is the difference between copy and strong?

 


For example:


@ 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 );

}

Assume that the address of the mStr object is 0x11, that is, 0x11 is the first address of @ "abc", and the address of the mStr variable itself in the memory is 0x123;

When mStr is assigned to retain's rStr, the address of the rStr object is 0x11, and the address of the rStr variable itself in the memory is 0x124; The rStr and mStr point to the same address, they point to the same object @ "abc". The address of this object is 0x11, so their values are the same.

When mStr is assigned to copy cStr, the cStr object address is 0x22, and the cStr variable's own memory address is 0x125; the cStr and mStr point to different addresses, they point to different objects, So copy is a deep copy, a new object, the address of this object is 0x22, the value is @ "abc ".

 


If you change the value of mStr:


[MStr appendString: @ "de"];


NSLog (@ "retainStr: % @", _ rStr );

NSLog (@ "copyStr: % @", _ cStr );


Result,
The rStr value of the retain string: @ "abcde ",
The value of the copy string cStr: @ "abc ",
Therefore, in general, we do not want the string value to change with mStr, So we generally use copy to set the string attribute.
If you want the string value to change with the value of the assigned string, you can use strong and retain.
Note: The above situation is only applicable when NSMutableString is assigned to NSString. If it is an NSString object, copy or strong is used, and the results are the same, because the NSString object cannot change its own value, it is immutable.


Assign an object to an attribute variable. When the object changes, use the strong attribute if you want the attribute variable to change. If you want the attribute variable to not change, use the copy attribute.

 


We can see that:
The source is a string of NSMutableString, and retain is only a pointer reference, adding a reference counter. When the source changes, variables declared in this retain method (whether the assigned variables are variable or immutable) will also change. The variables declared in copy will not change with the source, it is actually a deep copy.

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

In addition, both of them have the same impact on the memory count, and both will increase the memory reference count, which must be processed at the end.

To put it bluntly, why are the two methods used for strings? I think it is still a security issue, such as declaring an NSString * str variable, and then assigning the value of an NSMutableString * mStr variable to it. If you want str to change with mStr, use retain. If str cannot change with mStr, use copy. For strings of the NSString type to be assigned to str, there is no difference between the two. Security is not affected, and the same is true for memory management.

 


 

Related Article

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.