Day14 oc ARC

ARC Fundamentals

It is a compiler feature, the compiler finds the alloc object and inserts the release code. Unlike Java's garbage collection mechanism, it is a runtime mechanism. ARC's judgment mechanism: As long as there is no strong pointer to the object, the object will be released.

Strong pointers: by default all pointers are strong pointers __strong

Weak pointer: __weak

Use of ARC

When creating a project, check Automatic Reference Counting. During the development process, memory-related operations such as retain, release, and autorelease cannot be called.

#import<Foundation/Foundation.h>
@class Dog
@interface Person:NSObject
@property(nonatomic,strong) Dog *dog;//原为(nonatomic,retain)
@end

-(void)dealloc
{
//[_dog release]; No need to write this sentence
//[super dealloc]; This sentence does not allow calling
}

 PS:

1. It is not allowed to call release, retain, retainCount

2. Allow overriding dealloc, but not calling [supper alloc]

3. Parameters of @property

strong strong pointer, replace the original retain (applicable to OC objects)

week weak pointer

Original project converted to ARC

Edit-》Refactor-》Convert to object-c ARC

Check if the project is an ARC project

Click on the project build settings to find the object-c automatic reference counting option yes is arc

Set some files of the project to be non-arc (the project is compatible with both ARC and non-ARC)

Click on the project build phases - "compile source-" to select the non-arc file and double-click to fill in -fno-objc-arc

Or non-arc project settings arc fill in -f-objc-arc

ARC's circular references

 The solution is a strong week

Person.h   
@class Card  
@interface Person :NSObject  
@property(nonatomic,strong) Card *card;   
@end   
  
Card.h   
@class Person//Just tell the compiler that Person is a class  
@interface Card :NSObject  
@property(nonatomic,week) Person *person;   
@end

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326925352&siteId=291194637
arc