The difference between the iOS OC keyword copy,strong,weak,assign

Source: Internet
Author: User

First, introduce the differences between copy, strong, weak, as shown in the code

@property (copy,nonatomic) nsmutablestring*Acopymstr, @property (strong,nonatomic) nsmutablestring*Strongmstr, @property (weak,nonatomic) nsmutablestring*Weakmstr, @property (assign,nonatomic) nsmutablestring*Assignmstr; Nsmutablestring*mstrorigin = [[Nsmutablestringalloc] initwithstring:@"Mstroriginvalue"]; Self.acopymstr=Mstrorigin; Self.strongmstr=Mstrorigin; Self.weakmstr=Mstrorigin; NSLog (@"mstrorigin output:%p,%@\\n", Mstrorigin,mstrorigin); NSLog (@"acopymstr output:%p,%@\\n", _ACOPYMSTR,_ACOPYMSTR); NSLog (@"strongmstr output:%p,%@\\n", _STRONGMSTR,_STRONGMSTR); NSLog (@"weakmstr output:%p,%@\\n", _WEAKMSTR,_WEAKMSTR); NSLog (@"reference Count%@", [Mstroriginvalueforkey:@"Retaincount"]);//Output Results//2016-09-01 15:19:13.134 lbcopy[1205:87583] mstrorigin output: 0x7892a5e0,mstroriginvalue//2016-09-01 15:19:13.135 lbcopy[1205:87583] acopymstr output: 0x7893deb0,mstroriginvalue//2016-09-01 15:19:13.135 lbcopy[1205:87583] strongmstr output: 0x7892a5e0,mstroriginvalue//2016-09-01 15:19:13.135 lbcopy[1205:87583] weakmstr output: 0x7892a5e0,mstroriginvalue//2016-09-01 15:19:13.135 lbcopy[1205:87583] Reference count 2

Conclusion:

1, copy and strong reference counter plus one, weak reference counter does not add one.

2. The memory addresses of strong and weak point to mstrorigin,copy to create a new memory address and copy the contents, and then point to Mstrorigin.

Second, modify the value of the Mstrorigin, will not affect the ACOPYMSTR, only affect Strongmstr and weakmstr

Third, the mstrorigin to Nil,strong and weak are nil,copy not nil

Iv. Assign

The Assign reference counter does not add one, but when the point is set to NULL, only the value is released. This leads to the existence of a wild pointer, that is, when the address has not been written before other values, can output normal, but once the data is re-written, the pointer may not have a value at any time, causing the crash.

The difference between the iOS OC keyword copy,strong,weak,assign

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.