OC----内存管理

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/damys/article/details/89082663
-------------------------
- Person 类
-------------------------
@interface Person : NSObject
@property NSString *name;
 
- (void)say;
@end
 
@implementation Person
//相关于析构函数
- (void)dealloc{
    NSLog(@"名字叫%@的人挂了",_name);
    [super dealloc];
}
 
- (void)say{
    NSLog(@"say....");
}
@end
 
 
-------------------------
- 实现
-------------------------
Person *p1 = [[Person alloc] init];  //记数:1
p1.name = @"Jack";
 
NSInteger count = [p1 retainCount];   //调用记录器
NSLog(@"count=%lu", count);           //输出记录器结果:1
 
[p1 retain];                          //为对象发送retain 消息 对象的引用计数器就会+1
NSLog(@"count=%lu", p1.retainCount);  //2
 
 
[p1 release];                         //为对象发送release 消息.并不是回收对象.而是让对象的引用计数器-1
NSLog(@"count=%lu", p1.retainCount);  //1
 
[p1 release];   //记数:0.   当对象的引用计数器的值变为0的时候.对象才会被系统立即回收.

猜你喜欢

转载自blog.csdn.net/damys/article/details/89082663