Big Clock's iOS Development Tour (2) ———— A simple word about the variable modifiers of the property in the Arc and non-arc modes in iOS

Source: Internet
Author: User
Tags modifiers

/***********************************
 * author:[email protected] Big clock                                                                                        & nbsp                                  &NBS P  &NBSP
 * e-mail:[email protected]

 *site:http://www.idealpwr.com / 

 * Shenzhen Power Thinking Technology Development Co., Ltd.                                             &N Bsp                          ,         &NB Sp                        
 * http://blog.csdn.net/ conowen                                                      and nbsp                          ,         &NB Sp                   
 * Note: This article is original, only as a learning exchange use, reprint please indicate the author and source.     

********************************************************************************************/



First, preface

in iOS development, @property adornments have many modifiers, such as retain, assign, strong, weak, nonatomic, and so on, and these modifiers are slightly different in arc mode than in non-arc mode. Now let's talk about arc mode.



Ii. What is arc

ARC is a new feature of iOS 5, all called Automatic Reference counting. In other words, the code is automatically added to the retain and release, the original need to manually manage the reference count and manually release the count of the operation, the compiler can be completed automatically. For those of me who go from Android development to iOS, it's great to not worry about memory management or memory leaks.


Iii. Opening the ARC support


1, the use of Xcode 6.1 above the establishment of the project, the default is to open the ARC support




2, Arc and non-arc mixed

Due to the needs of the project, some non-arc third-party libraries may be used, and the following settings allow the project to be compatible with both arc and non-arc modes.

For non-Arc words, add a compile option after the code:-fno-objc-arc

Arc, add a compile option after the code:-fobjc-arc




It is also possible to convert non-ARC code into ARC type code, in fact, to remove some manual retain and release code.



Iv. simple differences between retain, assign, strong, weak and nonatomic

1. Arc mode

in general, the property variable modifier in arc mode is strong, weak, equivalent to retain, assign in non-arc mode ,

Strong: Instead of retain.

Weak: Instead of assign, but a function more than assign, the pointer to the address is freed, the pointer itself is automatically released.

e.g.

XX.h

@property (strong,nonatomic) nsstring *str1; @property (weak,nonatomic) NSString *str2;
XX.M

@synthesize str1; @synthesize str2;

-(void) viewdidload {    [super viewdidload];    Additional setup after loading the view, typically from a nib.    Str1=[nsmutablestring stringwithstring:@ "str1"];    NSLog (@ "str1=%p", str1);    STR2=STR1;    Str1=nil;    NSLog (@ "str2=%@", str2);    NSLog (@ "str2=%p", str2);    }

Log MESG: With weak, the pointer is automatically released without causing the wild pointer.

2014-11-04 17:37:59.763 helloworld[1184:426997] str1=0x15d6a4202014-11-04 17:37:59.767 Helloworld[1184:426997] str2= (null) 2014-11-04 17:37:59.768 helloworld[1184:426997] str2=0x0


If you make the following changes

XX.h

@property (strong,nonatomic) nsstring *str1; @property (strong,nonatomic) NSString *str2;

Log MESG:

2014-11-04 17:40:52.137 helloworld[1191:427491] str1=0x14522b902014-11-04 17:40:52.139 Helloworld[1191:427491] str2= str12014-11-04 17:40:52.140 helloworld[1191:427491] Str2=0x14522b90



(Wild pointer: the address that the pointer points to is released, the pointer itself is not released, this is the wild pointer)

2. Non-arc mode

Retain: When the variable is a pointer type, it needs to be retain decorated, and the reference series increases by 1 when using the Setter method (Assignment).

Assign: Modifies a variable of a non-pointer type and ID type, such as Nsinteger,cgfloat, or delegate


3. General mode

Atomic: atomicity, ensure multithreading security. Prevents the variable from being read by another thread when the write is incomplete, resulting in a data error, but consumes a certain amount of system resources.

Nonatomic: Non-atomic, multi-threaded, protected variable. Nonatomic can improve system performance without using communication programming between multiple threads.

Copy: The content copy of the variable, relative retain,retain is the pointer copy.



Big Clock's iOS Development Tour (2) ———— A simple word about the variable modifiers of the property in the Arc and non-arc modes in iOS

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.