// Person复合了Phone和Room
// 第一种
- (void)setPhone:(Phone *)phone
{
if (phone != _phone)
{
[_phone release];
_phone = [phone retain];
}
}
// 第二种
- (void)setRoom:(Room *)room
{
[room retain];
[_room release];
_room = room;
}
- (void)dealloc
{
NSLog(@"%s", __func__);
self.phone = nil; // 赋值为nil会调用setter完成下面的两步操作
// 等效的
[_room release];
_room = nil;
[super dealloc];
}
时间: 2024-11-03 07:21:29