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对象类型(intfloatenumstruct40 @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