关于什么时间用self. ,其实是和Obj-c的存取方法有关,不过网上很多人也都这么解答的,那它为什么和存取方法有关?怎么有关的?并没有多少人回答出来.同时关于内存管理的内容,请大家看旺财勇士的Objective-C内存管理总结~CC专版,有些东西我就不多解释了.<wbr style="line-height:25px"></wbr>
进入正题,我们经常会在官方文档里看到这样的代码:<wbr style="line-height:25px"></wbr>
MyClass.h<wbr style="line-height:25px"></wbr>
[/lang]<wbr style="line-height:25px"></wbr>
@interface MyClass : NSObject {<wbr style="line-height:25px"></wbr>
MyObject *myObject;<wbr style="line-height:25px"></wbr>
}<wbr style="line-height:25px"></wbr>
@property (nonatomic, retain) MyObject *myObject;<wbr style="line-height:25px"></wbr>
@end<wbr style="line-height:25px"></wbr>
<wbr style="line-height:25px"></wbr>
<wbr style="line-height:25px"></wbr>
MyClass.m<wbr style="line-height:25px"></wbr>
<wbr style="line-height:25px"></wbr>
@synthesize myObject;<wbr style="line-height:25px"></wbr>
<wbr style="line-height:25px"></wbr>
-(id)init{<wbr style="line-height:25px"></wbr>
if(self = [super init]){<wbr style="line-height:25px"></wbr>
MyObject * aMyObject = [[MyObject alloc] init];<wbr style="line-height:25px"></wbr>
self.myObject = aMyObject;<wbr style="line-height:25px"></wbr>
[aMyObject release];<wbr style="line-height:25px"></wbr>
}<wbr style="line-height:25px"></wbr>
return self;<wbr style="line-height:25px"></wbr>
}<wbr style="line-height:25px"></wbr>
<wbr style="line-height:25px"></wbr>
有人就问,为什么要这么复杂的赋值?为什么要加self. ?直接写成self.myObject = [[MyObject alloc] init];不是也没有错么?不加self有时好像也是正常的?<wbr style="line-height:25px"></wbr>
<wbr style="line-height:25px"></wbr>
现在我们来看看内存管理的内容:<wbr style="line-height:25px"></wbr>
<wbr style="line-height:25px"></wbr>
先看间接赋值的:<wbr style="line-height:25px"></wbr>
1.加self.:<wbr style="line-height:25px"></wbr>
<wbr style="line-height:25px"></wbr>
MyObject * aMyObject = [[MyObject alloc] init]; //aMyObject retainCount = 1;<wbr style="line-height:25px"></wbr>
self.myObject = aMyObject; //myObject retainCount = 2;<wbr style="line-height:25px"></wbr>
[aMyObject release];//myObject retainCount = 1;<wbr style="line-height:25px"></wbr>
<wbr style="line-height:25px"></wbr>
<wbr style="line-height:25px"></wbr>
2.不加self.:<wbr style="line-height:25px"></wbr>
<wbr style="line-height:25px"></wbr>
MyObject * aMyObject = [[MyObject alloc] init]; //aMyObject retainCount = 1;<wbr style="line-height:25px"></wbr>
myObject = aMyObject; //myObject retainCount = 1;<wbr style="line-height:25px"></wbr>
[aMyObject release];//对象己经被释放<wbr style="line-height:25px"></wbr>
<wbr style="line-height:25px"></wbr>
<wbr style="line-height:25px"></wbr>
再看直接赋值的:<wbr style="line-height:25px"></wbr>
3.加self.:<wbr style="line-height:25px"></wbr>
<wbr style="line-height:25px"></wbr>
self.myObject = [[MyObject alloc] init]; //myObject retainCount = 2;<wbr style="line-height:25px"></wbr>
<wbr style="line-height:25px"></wbr>
<wbr style="line-height:25px"></wbr>
4.不加self.:<wbr style="line-height:25px"></wbr>
<wbr style="line-height:25px"></wbr>
myObject = [[MyObject alloc] init]; //myObject retainCount = 1;<wbr style="line-height:25px"></wbr>
<wbr style="line-height:25px"></wbr>
<wbr style="line-height:25px"></wbr>
现在是不是有点晕,我们先来把代码改一下,官方的一种常见写法:<wbr style="line-height:25px"></wbr>
<wbr style="line-height:25px"></wbr>
MyClass.h<wbr style="line-height:25px"></wbr>
<wbr style="line-height:25px"></wbr>
@interface MyClass : NSObject {<wbr style="line-height:25px"></wbr>
MyObject * _myObject;<wbr style="line-height:25px"></wbr>
}<wbr style="line-height:25px"></wbr>
@property (nonatomic, retain) MyObject *myObject;<wbr style="line-height:25px"></wbr>
@end<wbr style="line-height:25px"></wbr>
<wbr style="line-height:25px"></wbr>
<wbr style="line-height:25px"></wbr>
MyClass.m<wbr style="line-height:25px"></wbr>
<wbr style="line-height:25px"></wbr>
@synthesize myObject = _myObject;<wbr style="line-height:25px"></wbr>
<wbr style="line-height:25px"></wbr>
<wbr style="line-height:25px"></wbr>
OK,你现在再试下,如果你用self._myObject = aMyObject;或者myObject = aMyObject;你会得到一个错误,为什么呢,这里就是和Obj-c的存取方法有关了.说白了很简单,大家都知道, @property (nonatomic, retain) MyObject *myObject;是为一个属性设置存取方法,只是平时我们用的方法名和属性名是一样的,现在你把它写成不同的名字,就会很清楚了. _myObject是属性本身, myObject是存取方法名.<wbr style="line-height:25px"></wbr>
一般来说self点出来的都是方法这个好办,
但是标准的TAB项目的Delegate的m文件里会出现这样的代码
@synthesize window = _window;
@synthesize tabBarController = _tabBarController;
- (void)dealloc
{
[_window release];
[_tabBarController release];
[super dealloc];
}之前一直很晕的问题是为什么释放的是_window 而不是window 也查过的问题是window = _window这种但是很多地方的解释是window是_window的别名,或者说把window定义成_window的别名。但是这里的解释能够更清楚地解答我的疑问了,@property和@synthesize是声明是实现一个属性的存取方法。@property是定义属性说明需要存取方法 @synthesize是合成存取器是告诉编译器生成属性的get和set方法。以往的感觉是定义了属性就会出现与之同名的变量 但是现在来看其实只是有个方法,如果没有定义或者赋值到本体的内部成员变量上那么会自动成圣与之同名的变量,如果赋值了那么则证明 如window = _window则说明属性window实际操作的是内部成员的_window. 也就是说 @property定义的是方法名@synthesize是让编译器自动生成方法。同时这里还解决了我一个问题就是关于readonly只读属性的赋值问题,尤其是这种带这window=_window的,现在来看是不生成SET方法,而读取方法返回的实际_window。
现在我们知道self.是访问属性的存取方法了,那存取方法又怎么工作的? self.myObject = [[MyObject alloc] init];为什么会有内存泄露?<wbr style="line-height:25px"></wbr>
关于nonatomic我不多解释了,它不是我要讲的重点,而且我也没完全搞清楚,不误导大家.
关于nonatomic我这也是一敲不通啊,目前还没用过atomicity模式的属性。
我只说assign, retain ,copy.<wbr style="line-height:25px"></wbr>
get方法是:<wbr style="line-height:25px"></wbr>
<wbr style="line-height:25px"></wbr>
-(MyObject*)myObject{<wbr style="line-height:25px"></wbr>
return _myObject;<wbr style="line-height:25px"></wbr>
}<wbr style="line-height:25px"></wbr>
<wbr style="line-height:25px"></wbr>
<wbr style="line-height:25px"></wbr>
Set方法是:<wbr style="line-height:25px"></wbr>
<wbr style="line-height:25px"></wbr>
// assign<wbr style="line-height:25px"></wbr>
-(void)setMyObject:(id)newValue{<wbr style="line-height:25px"></wbr>
_myObject = newValue;<wbr style="line-height:25px"></wbr>
}<wbr style="line-height:25px"></wbr>
// retain<wbr style="line-height:25px"></wbr>
-(void)setMyObject:(id)newValue{<wbr style="line-height:25px"></wbr>
if (_myObject != newValue) {<wbr style="line-height:25px"></wbr>
[_myObject release];<wbr style="line-height:25px"></wbr>
_myObject = [newValue retain];<wbr style="line-height:25px"></wbr>
}<wbr style="line-height:25px"></wbr>
}<wbr style="line-height:25px"></wbr>
// copy<wbr style="line-height:25px"></wbr>
-(void)setMyObject:(id)newValue{<wbr style="line-height:25px"></wbr>
if (_myObject != newValue) {<wbr style="line-height:25px"></wbr>
[_myObject release];<wbr style="line-height:25px"></wbr>
_myObject = [newValue copy];<wbr style="line-height:25px"></wbr>
}<wbr style="line-height:25px"></wbr>
}<wbr style="line-height:25px"></wbr>
<wbr style="line-height:25px"></wbr>
<wbr style="line-height:25px"></wbr>
其实这些方法里还有别的内容,并不只是这些.而且这些方法可以被重写.比如你写一个<wbr style="line-height:25px"></wbr>
<wbr style="line-height:25px"></wbr>
-(MyObject*)myObject{<wbr style="line-height:25px"></wbr>
<wbr style="line-height:25px"></wbr>
return _myObject;<wbr style="line-height:25px"></wbr>
}<wbr style="line-height:25px"></wbr>
<wbr style="line-height:25px"></wbr>
<wbr style="line-height:25px"></wbr>
放在你的类里,你调用self.myObject时(不要把它放在等号左边,那会调用get方法)就会调用这个方法.<wbr style="line-height:25px"></wbr>
<wbr style="line-height:25px"></wbr>
这里多说一句, @property是为你设置存取方法,和你的属性无关,你可以只写一句<wbr style="line-height:25px"></wbr>
看来理解对了 和属性本身无关 就是定义的访问方法名
<wbr style="line-height:25px"></wbr>
@property (readonly) NSString *name;<wbr style="line-height:25px"></wbr>
<wbr style="line-height:25px"></wbr>
<wbr style="line-height:25px"></wbr>
在你的类里实现<wbr style="line-height:25px"></wbr>
<wbr style="line-height:25px"></wbr>
-(NSString*)name{<wbr style="line-height:25px"></wbr>
NSLog(@"name");<wbr style="line-height:25px"></wbr>
return @"MyClass";<wbr style="line-height:25px"></wbr>
}<wbr style="line-height:25px"></wbr>
<wbr style="line-height:25px"></wbr>
<wbr style="line-height:25px"></wbr>
同样可以用self.name调用.<wbr style="line-height:25px"></wbr>
<wbr style="line-height:25px"></wbr>
现在回头说说我们开始的那四个赋值,当不用self.的时候,那句话只是一般的赋值,把一个指针赋给另一个指针,不会对分配的内存有任何影响,所以2中不要最后[aMyObject release];这句话和4是一回事.这里就不多说了.我们看看1和3,<wbr style="line-height:25px"></wbr>
当调用setMyObject:方法时,对newValue做了一次retain操作,我们必须把原来的newValue释放掉,不然就会内存泄露,在1里,我们有个aMyObject可以用来释放,在3里,我们无法释放它,所以,在3里,我们会多出来一个retainCount.内存泄露了.<wbr style="line-height:25px"></wbr>
self.myObject = [[MyObject alloc] init]会造成的内存泄露是说self点的时候一个引用然后alloc以后又会有一个 所以在这里一下增加的是两个引用计数。<wbr style="line-height:25px"></wbr>
说了这么多,我只想让大家清楚,什么是调用属性本身,什么是调用存取方法.怎么样才能避免内存泄露,而且,以上例子里是在自己类里的调用,如果这个类被别的