Retain, strong, and copy tests

Source: Internet
Author: User

It's a little confusing from time to time. Retain, strong, copy the difference between the three, or to record the better, first look at the code:

Create a class that defines a property

#import <Foundation/Foundation.h> @interface Person:nsobject@property (nonatomic, retain) nsstring *strretain;@ Property (Nonatomic, Strong) NSString *strstrong; @property (nonatomic, copy) NSString *strcopy; @property (Nonatomic, Retain) nsmutablestring *strmutableretain, @property (nonatomic, strong) nsmutablestring *strmutablestrong; @property ( nonatomic, copy) nsmutablestring *strmutablecopy; @end

The code inside the Main method:

Person *per = [[Person alloc] init];    NSString *name = @ "Original XM";    [Per Setstrretain:name];    [Per Setstrstrong:name];        per.strcopy = name;    nsmutablestring *name2 = [[Nsmutablestring alloc] initwithstring:@ "original XM2"];    Per.strmutableretain = name2;    Per.strmutablestrong = name2;        Per.strmutablecopy = name2;    NSLog (@ "------print------before changing");    NSLog (@ "Per.strretain:%@,", Per.strretain);    NSLog (@ "Per.strstrong:%@", Per.strstrong);    NSLog (@ "per.strcopy:%@", per.strcopy);    NSLog (@ "Per.strmutableretain:%@", Per.strmutableretain);    NSLog (@ "Per.strmutablestrong:%@", Per.strmutablestrong);        NSLog (@ "per.strmutablecopy:%@", per.strmutablecopy);    Name = @ "New XM";        [Name2 appendstring:@ "New XM2"];    NSLog (@ "Print-------after-------change");    NSLog (@ "Per.strretain:%@", Per.strretain);    NSLog (@ "Per.strstrong:%@", Per.strstrong);    NSLog (@ "per.strcopy:%@", per.strcopy);    NSLog (@ "Per.strmutableretain:%@", Per.strmutableretain); NSLog (@ "Per.strmutablesTrong:%@ ", Per.strmutablestrong); NSLog (@ "per.strmutablecopy:%@", per.strmutablecopy);

Printing results:

------Change before printing------Per.strretain: Original XM, Per.strstrong: Original xmper.strcopy: Original xmper.strmutableretain: Original Xm2per.strmutablestrong: Original xm2per.strmutablecopy: Original XM2-------changed after printing-------Per.strretain: Original xmper.strstrong: Original xmper.strcopy: Original Xmper.strmutableretain: Original XM2 new Xm2per.strmutablestrong: Original XM2 new xm2per.strmutablecopy: Original XM2

Conclusion:

1, for non-mutable objects: Retain, strong, copy of the role is the same, that is, when the reference value of the original object is changed, other references to the object's property values will not be affected, or maintain the original value;

2, for mutable objects: retain, strong and copy of the role of the difference, using the retain, strong decorated properties, when the reference value of the original object changed, the other reference to the object's property values will be changed together, and the value of the copy decorated property remains intact. The role of copy is mainly reflected here: let the value of the property will not change with the value of the original reference object;

3, retain and strong difference: the role is the same, just the difference between the wording. In the non-arc mechanism, it is modified with the Retain keyword, and after the arc mechanism, the strong keyword is used instead of the retain.

Retain, strong, and copy tests

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.