Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Compiler feature ARC

2025-02-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)06/01 Report--

ARC (is a compiler feature)

ARC is a new feature added since iOS 5, which completely eliminates the hassle of managing memory manually, and the compiler automatically inserts the appropriate retain, release, and autorelease statements where appropriate. You no longer need to worry about memory management, because the compiler handles everything for you.

ARC is a compiler feature, not an iOS runtime feature, nor is it similar to a garbage collector in other languages. So ARC and manual memory management have the same performance, and sometimes faster, because the compiler can also perform some optimizations

ARC fundamentals (not a garbage collection mechanism like java) the rules for ARC are simple: as long as there is a strong pointer variable to the object, the object remains in memory

Strong and weak pointers

By default all instance variables and local variables are Strong pointers

After the object pointed to by the weak pointer is recycled, the weak pointer will automatically become a nil pointer and will not cause a wild pointer error

ARC judgment rule: as long as there is no strong pointer to the object, the object will be released, the weak pointer will not be like this, even if there is a weak pointer to the object, the object will be automatically released without a strong pointer. In general, you don't need to explicitly declare a strong pointer, but in an encapsulation, you need to specify when you define a method. Weak pointers, however, must be explicitly stated. The default is a strong pointer.

Characteristics of ARC

1 > calls to release, retain, retainCount are not allowed

2 > dealloc rewriting is allowed, but calls to [super dealloc] are not allowed

Parameters of 3 > @ property

* strong: member variables are strong pointers (for OC object types)

* weak: member variables are weak pointers (applicable to OC object types)

* assign: applicable to non-OC object types

4 > the previous retain was changed to strong

There are 2 types of pointers for oc:

1 > strong pointer: by default, all pointers are strong pointers _ _ strong

2 > weak pointer: _ _ weak

/ * File name: Dog.h * / # import @ interface Dog: NSObject@end/* File name: Dog.m * / # import "Dog.h" @ implementation Dog- (void) dealloc {NSLog (@ "Dog is dealloc");} @ end/* File name: Person.h * / # import @ class Dog;@interface Person: NSObject@property (nonatomic, strong) Dog * dog;@property (nonatomic, strong) NSString * name;@property (nonatomic, assign) int age @ end/* file name: Person.m * / # import "Person.h" @ implementation Person- (void) dealloc {NSLog (@ "Person is dealloc"); / / [super dealloc]; cannot be written, otherwise an error will be reported} @ end// main.m#import # import "Person.h" # import "Dog.h" int main () {Dog * d = [[Dog alloc] init]; Person * p = [Person alloc] init]; p.dog = d D = nil; NSLog (@ "% @", p.dog); return 0;} void test () {/ / incorrectly written (meaningless) _ _ weak Person * p = [[Person alloc] init]; NSLog (@ "% @", p); NSLog (@ "-");}

Refactoring old code (manual memory management is refactored to ARC) xcode6

After doing this, you can convert non-ARC projects into ARC projects.

How do I check if the project is ARC?

Search for auto in build settings and see the options:

How do you make ARC and non-ARC coexist in the same project?

You often need to use a third-party framework, or some other old code, so what if some support ARC and some don't? You can set it like this: in the compilation options

Double-click the file that needs to be non-ARC, set as follows:

-fno-objc-arc

So that this file can use keywords such as retain, release,autorelease, etc.

-f stands for the meaning of the flags mark and is written in a fixed way.

Conversely, for non-ARC projects, set this:

-f-objc-arc

Note when using ARC

Cannot call release, retain, autorelease, retainCount

You can override dealloc, but you cannot call [super dealloc]

@ property: if you want to own an object for a long time, you should use strong and other objects use weak

Other basic data types still use assign

When two ends refer to each other, one end uses strong and the other end uses weak

Similarly, in ARC projects, there is also a phenomenon of circular double-ended references, you strong me, I strong your situation. The solution remains the same. When two ends refer to each other, one end uses strong and the other end uses weak

/ * File name: Dog.h * / # import @ class Person;@interface Dog: NSObject@property (nonatomic, weak) Person * person;@end/* File name: Dog.m * / # import "Dog.h" @ implementation Dog- (void) dealloc {NSLog (@ "Dog--dealloc");} @ end/* File name: Person.h * / # import @ class Dog;@interface Person: NSObject@property (nonatomic, strong) Dog * dog @ end/* filename: Person.m * / # import "Person.h" @ implementation Person- (void) dealloc {NSLog (@ "Person--dealloc") } @ end// main.m#import # import "Person.h" # import "Dog.h" / * when both ends are referenced in a loop, the solution: 1 > ARC 1 uses strong, the other side uses weak 2 > non-ARC 1 side uses retain, the other end uses assign * / int main () {Person * p = [[Person alloc] init]; Dog * d = [Dog alloc] init]; p.dog = d; d.person = p; return 0;}

Otherwise, the error will also be reported, such as using the strong attribute.

Person * p = [[Person alloc] init]; Dog * d = [[Dog alloc] init]

Memory layout:

P.dog = d dog / assign the dog object to the _ pointer in the person object, which is a strong pointer.

D.person = p _ person / again, the _ person strong pointer in the person object points to the person object

When the program is finished, or the main function is finished, the automatic variable is destroyed.

Because they are all strong pointers, as mentioned above, memory leaks occur. Therefore, the _ _ weak or weak attribute is generally used in circular references, but it is rare in other situations.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report