1,什么是循环retain?
两个类之间互相包含;
例如:Dog中retain了一个Person,Person中retain了一个Dog;
@interface Dog : NSObject
@property (nonatomic,retain) Person *owner;
@end
@interface Person : NSObject
@property (nonatomic,retain) Dog *dog;
@end
Person *p = [Person new];
Dog *d = [Dog new];
p.dog = d;
d.owner = p;
2,如何解决循环retain?
在两端的set方法中,有一端必须使用assign直接赋值,不能用retain;在@property中参数使用assign。
例如:
@interface Dog : NSObject
@property (nonatomic,assign) Person *owner;
@end
@interface Person : NSObject
@property (nonatomic,retain) Dog *dog;
@end
Person *p = [Person new];
Dog *d = [Dog new];
p.dog = d;
d.owner = p; |
|