我正在阅读保留周期,"保留周期可以采取一些形式,但它通常意味着对象A保留对象B,对象B保留对象A,但没有其他任何东西保留对象A或B".但我不清楚这些概念.请有人解释保留周期与现实世界的例子.
谢谢.
一个简单的例子,一个人住在一个部门,一个部门有一个人(假设有一个人)
@class Department; @interface Person:NSObject @property (strong,nonatomic)Department * department; @end @implementation Person -(void)dealloc{ NSLog(@"dealloc person"); } @end @interface Department: NSObject @property (strong,nonatomic)Person * person; @end @implementation Department -(void)dealloc{ NSLog(@"dealloc Department"); } @end
然后这样称呼它
- (void)viewDidLoad { [super viewDidLoad]; Person * person = [[Person alloc] init]; Department * department = [[Department alloc] init]; person.department = department; department.person = person; }
你不会看到dealloc日志,这是保留圈