Day12 oc circular reference

circular reference

Specification for referencing a class in development

1. Declare the class with @class in .h

2. Use #import in .m to include all classes

Person.h
@class Card//Only @class in the header file, used to declare a class in the header file, compared to import not all imports
@interface Person :NSObject
@property(nonatomic,retain) Card *card;
@end

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

Card.m
import "Person.h"//Import it when it is really used
@implementation Card
-(void)dealloc
{
[_person release];
[super dealloc];
}
@end

 Note: Except for inherited classes (such as import<Foundation/Foundation.h>) in the header file, the rest do not need to be imported, because it will reduce the compilation efficiency.

When used, it causes a circular retain, A retains B, and B retains A, as follows

Person *p = [[Person alloc] init];
Card * c = [[Card alloc] init];
p.card = c;
c.person = p;
[p release];
[c release];

 Circular reference at both ends, solution

1. Use retain at one end

2. Use assign on one end

Guess you like

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