首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

property跟_property的一些小结

2012-09-24 
property和_property的一些小结?在.h文件中:@interface MyClass:NSObject{? ?   MyObjecct *_myObject}@p

property和_property的一些小结

?

在.h文件中:

@interface MyClass:NSObject

{

? ?   MyObjecct *_myObject;

}

@property(nonamtic, retain) MyObjecct *myObject;

@end

?

在.m文件中

@implementation?MyClass

?

@synthesize myObject=_myObject;

?

?

- (void)dealloc

{

? ? [_myObject release];

? ? [super dealloc];

}

?

@end


通过最近google了一些文章,如果大家观后有自己看法希望能留言_myObject是实例变量,相当于C++中self->myObjectself.myObject相当于[self myObject];是一个消息,也有说是属性http://stackoverflow.com/questions/5466496/why-rename-synthesized-properties-in-ios-with-leading-underscores
myObject和_myObject是用来区别实例变量消息。一般来说,应该使用setter和getter的属性,而不是直接访问实例变量。当你写一个setter方法,该方法不能自身使用setter,否则你会产生无限递归,并且发生崩溃。setter调用自身,再次调用自身,直到堆栈溢出。http://www.iphonedevsdk.com/forum/iphone-sdk-development/98273-feeling-confused-about-_property-and-property.html
写成这样:
在init和dealloc方法中最好用_myObject,因为这样直接初始化或销毁内存,比self.myObject这样调用引用要效率高,
在其他方法中就self.myObject调用引用进行操作了。
至于建议在dealloc中加_myObject =nil就是在销毁的时候直接将其引用基数归0,就是回收内存嘛,呵呵



--希望对你有帮助 4 楼 synchronized_lala 2012-09-08   白色蜻蜓 写道myObject=_myObject,前者是引用,后者是内存。
在init和dealloc方法中最好用_myObject,因为这样直接初始化或销毁内存,比self.myObject这样调用引用要效率高,
在其他方法中就self.myObject调用引用进行操作了。
至于建议在dealloc中加_myObject =nil就是在销毁的时候直接将其引用基数归0,就是回收内存嘛,呵呵



--希望对你有帮助




嗯嗯,有帮助,因为oc的知识点没有系统学过,所以对你一开始的话不是很理解,现在看了一点,总算是有进步啊。还有虽然对在dealloc中加_myObject =nil来直接将其引用基数归0,我理解了,但是还没有代码上的体会。我要继续努力。O(∩_∩)O谢谢! 5 楼 synchronized_lala 2012-09-09   白色蜻蜓 写道myObject=_myObject,前者是引用,后者是内存。
在init和dealloc方法中最好用_myObject,因为这样直接初始化或销毁内存,比self.myObject这样调用引用要效率高,
在其他方法中就self.myObject调用引用进行操作了。
至于建议在dealloc中加_myObject =nil就是在销毁的时候直接将其引用基数归0,就是回收内存嘛,呵呵



--希望对你有帮助



问下下面的解释对吗?今天看到的觉得好像懂了一些
1、加self的方式:
Student *mystudent = [[Student alloc] init];      //mystudent 对象 retainCount = 1;
self.student = mystudent;   //student 对象 retainCount = 2;
[mystudent release];  //student 对象 retainCount = 1;
retainCount指对象引用计数,student的property 是retain 默认使用self.student引用计数+1。
2、不加self的方式
Student *mystudent = [[Student alloc] init];       //mystudent 对象 retainCount = 1;
student = mystudent;   //student 对象 retainCount = 1;
[mystudent release];   //student 对象内存已释放,如果调用,会有异常
3、加self直接赋值方式
self.student = [[Student alloc] init]; //student 对象 retainCount = 2;容易造成内存泄露
由于objective-c内存管理是根据引用计数处理的,当一个对象的引用计数为零时,gcc才会释放该内存。 6 楼 白色蜻蜓 2012-09-10   synchronized_lala 写道白色蜻蜓 写道myObject=_myObject,前者是引用,后者是内存。
在init和dealloc方法中最好用_myObject,因为这样直接初始化或销毁内存,比self.myObject这样调用引用要效率高,
在其他方法中就self.myObject调用引用进行操作了。
至于建议在dealloc中加_myObject =nil就是在销毁的时候直接将其引用基数归0,就是回收内存嘛,呵呵



--希望对你有帮助



问下下面的解释对吗?今天看到的觉得好像懂了一些
1、加self的方式:
Student *mystudent = [[Student alloc] init];      //mystudent 对象 retainCount = 1;
self.student = mystudent;   //student 对象 retainCount = 2;
[mystudent release];  //student 对象 retainCount = 1;
retainCount指对象引用计数,student的property 是retain 默认使用self.student引用计数+1。
2、不加self的方式
Student *mystudent = [[Student alloc] init];       //mystudent 对象 retainCount = 1;
student = mystudent;   //student 对象 retainCount = 1;
[mystudent release];   //student 对象内存已释放,如果调用,会有异常
3、加self直接赋值方式
self.student = [[Student alloc] init]; //student 对象 retainCount = 2;容易造成内存泄露
由于objective-c内存管理是根据引用计数处理的,当一个对象的引用计数为零时,gcc才会释放该内存。
不错,大体理解上没有任何问题,对此做出以下几点补充
1、你可以用[self.student retainCount]打印出引用基数可以看到该基数的确切数值,当然,很熟练就没有必要了。
2、针对你的第二条,这个不加self的sutdent应该是你定义了变量,但是没有对其添加@property 和@synthesize吧。总之,你alloc了一下,又release了一下,基数归0,就不能再引用了,如非要这样可以[student retain]一下 即可调用
3、对于你说的第三条我们一般都这样操作self.student =[ [[Student alloc] init] autoreleae]; 这样autorelease一下,就不会泄漏了。
4、总之,有alloc、copy、retain的地方你就肯定要用一个release或者autorelease与相对应,这就是oc内存管理的黄金法则,当然了,切实的体会还需在代码中多多练习。

--希望对你有帮助,有疑惑得地方再行讨论。 7 楼 synchronized_lala 2012-09-11   白色蜻蜓 写道synchronized_lala 写道白色蜻蜓 写道myObject=_myObject,前者是引用,后者是内存。
在init和dealloc方法中最好用_myObject,因为这样直接初始化或销毁内存,比self.myObject这样调用引用要效率高,
在其他方法中就self.myObject调用引用进行操作了。
至于建议在dealloc中加_myObject =nil就是在销毁的时候直接将其引用基数归0,就是回收内存嘛,呵呵



--希望对你有帮助



问下下面的解释对吗?今天看到的觉得好像懂了一些
1、加self的方式:
Student *mystudent = [[Student alloc] init];      //mystudent 对象 retainCount = 1;
self.student = mystudent;   //student 对象 retainCount = 2;
[mystudent release];  //student 对象 retainCount = 1;
retainCount指对象引用计数,student的property 是retain 默认使用self.student引用计数+1。
2、不加self的方式
Student *mystudent = [[Student alloc] init];       //mystudent 对象 retainCount = 1;
student = mystudent;   //student 对象 retainCount = 1;
[mystudent release];   //student 对象内存已释放,如果调用,会有异常
3、加self直接赋值方式
self.student = [[Student alloc] init]; //student 对象 retainCount = 2;容易造成内存泄露
由于objective-c内存管理是根据引用计数处理的,当一个对象的引用计数为零时,gcc才会释放该内存。
不错,大体理解上没有任何问题,对此做出以下几点补充
1、你可以用[self.student retainCount]打印出引用基数可以看到该基数的确切数值,当然,很熟练就没有必要了。
2、针对你的第二条,这个不加self的sutdent应该是你定义了变量,但是没有对其添加@property 和@synthesize吧。总之,你alloc了一下,又release了一下,基数归0,就不能再引用了,如非要这样可以[student retain]一下 即可调用
3、对于你说的第三条我们一般都这样操作self.student =[ [[Student alloc] init] autoreleae]; 这样autorelease一下,就不会泄漏了。
4、总之,有alloc、copy、retain的地方你就肯定要用一个release或者autorelease与相对应,这就是oc内存管理的黄金法则,当然了,切实的体会还需在代码中多多练习。

--希望对你有帮助,有疑惑得地方再行讨论。








嗯嗯,retainCount我试了一下,挺好用的,还有你之前说的_object = nil;我也改过来了,非常感谢啊!(*^__^*) 嘻嘻……

热点排行