1 @class
@class ClassName, just tell the editor that ClassName is a class
Avoid circular copying
Using the @class declaration in. h
Import header files in. m files
2 Circular Reference problems
2.1 Definition:
Refers to two objects retain each other, through release is unable to destroy the two objects
2.2 Description:
For example, two classes of objects A and B are created in the main function, and the reference count is now 1.
Now let A and B refer to each other, and the reference count of two objects increases by 1 and becomes 2.
execution [A release]; The [B release];main function releases its own object, but a and B reference each other, and the reference count is still 1.
At this point A and B will not be released, because to release a you must first release B, and then release a in the Dealloc method of B.
Similarly, to release B, you must first release a, and then release B in the Dealloc method of a.
So these two objects will always be in memory without releasing, which is the circular reference problem.
2.3 Solutions
In general, you can set the reference property of a short life cycle to assign. Or a, B are set to assign
Examples of circular references:
Person.h@class car@interface Person@property (noatomic, retain) car* Car; @end
Person.m#import "Person.h" @implementation person-(void) Dealloc{[_car Release];_car = nil;[ Super Dealloc];} @end
Car.h@class person@interface Car@property (noatomic, retain) person* person; @end
Car.m#import "Car.h" @implementation car-(void) Dealloc{[_person release];[ Super Dealloc];} @end
Main.m#import "Car.h" #import "Person.h" int main () {person* person = [[Person alloc] init];//person 1car* Car = [[Car al] LOC] init];//Car 1person.car = car;//car 2car.person = person;//person 2[car release];//car 1[person release];//Perso n 1return 0;}
Workaround Example:
Car.h@class person@interface Car@property (noatomic, assign) person* person; @end
Car.m#import "Car.h" @implementation car-(void) dealloc{[super dealloc];} @end
Main.m#import "Car.h" #import "Person.h" int main () {person* person = [[Person alloc] init];//person 1car* Car = [[Car al] LOC] init];//Car 1person.car = car;//car 2car.person = person;//person 1[car release];//car 1[person release];//Perso n 0,car 0return 0;}
iOS Review Note 7: Circular reference issues