oc-内存管理总结

 1 一、计数器的基本操作
 2 1> retain : +1
 3 2> release :-1
 4 3> retainCount : 获得计数器
 5 
 6 二、set方法的内存管理
 7 1> set方法的实现
 8 - (void)setCar:(Car *)car
 9 {
10     if ( _car != car )
11     {
12         [_car release];
13         _car = [car retain];
14     }
15 }
16 
17 2> dealloc方法的实现(不要直接调用dealloc)
18 - (void)dealloc
19 {
20     [_car release];
21     [super dealloc];
22 }
23 
24 三、@property参数
25 1> OC对象类型
26 @property (nonatomic, retain) 类名 *属性名;
27 @property (nonatomic, retain) Car *car;
28 @property (nonatomic, retain) id car;
29 
30 添加的retain,相当于编译器为我们添加了总结二的set方法,减少程序员编写重复代码;
31 
32 // 被retain过的属性,必须在dealloc方法中release属性
33 - (void)dealloc
34 {
35     [_car release];
36     [super dealloc];
37 }
38 
39 2> 非OC对象类型(int\float\enum\struct40 @property (nonatomic, assign) 类型名称 属性名;
41 @property (nonatomic, assign) int age;
42 
43 四、autorelease
44 1.系统自带的方法中,如果不包含alloc、new、copy,那么这些方法返回的对象都是已经autorelease过的
45 [NSString stringWithFormat:....];
46 [NSDate date];
47 
48 2.开发中经常写一些类方法快速创建一个autorelease的对象
49 * 创建对象的时候不要直接使用类名,用self

 

转载于:https://www.cnblogs.com/My-Cloud/p/4502071.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值