WEB开发网
开发学院手机开发iPhone 开发 关于Ojbective-C的self.的用法总结 阅读

关于Ojbective-C的self.的用法总结

 2010-10-19 20:45:32 来源:WEB开发网   
核心提示:这里自己再进行编辑下,希望日后能看得更清楚些,关于Ojbective-C的self.的用法总结,也希望看原贴不是太清楚的同鞋,能看了我的理解

这里自己再进行编辑下,希望日后能看得更清楚些,也希望看原贴不是太清楚的同鞋,能看了我的理解,有更多了解:

MyObject文件:

/*MyObject*/

//MyObject.h

@interface MyObject : NSObject {}

@end

//MyObject.M

@implementation MyObject

@end

MyClass文件:

代码

/*MyClass*/

//MyClass.h

@interface MyClass : NSObject {

@public

MyObject * _myObject;

}

@property (nonatomic, retain) MyObject *myObject;

@end

//MyClass.m

@implementation MyClass

@synthesize myObject = _myObject;

-(id)init{

if(self = [super init]){

MyObject * aMyObject = [[MyObject alloc] init]; //aMyObject retainCount = 1;

NSLog(@"1:aMyobject:%d",[aMyObject retainCount]);

self.myObject = aMyObject; //myObject retainCount = 2;

NSLog(@"2:aMyobject:%d",[aMyObject retainCount]);

[aMyObject release];//myObject retainCount = 1;

NSLog(@"3:aMyobject:%d",[aMyObject retainCount]);

_myObject = [[MyObject alloc] init];

NSLog(@"21:aMyobject:%d",[_myObject retainCount]);

self.myObject = [[MyObject alloc] init];

NSLog(@"22:aMyobject:%d",[_myObject retainCount]);

}

return self;

}

测试代码:

代码

/*对象引用对象时,使用存取方法和使用对象属性的区别*/

MyClass* class = [[MyClass alloc] init];

class.myObject = [[MyObject alloc] init];

NSLog(@"31: '.' class object count %d", [class.myObject retainCount] );

class->_myObject = [[MyObject alloc] init];

NSLog(@"32: '->' class object count %d", [class.myObject retainCount] );

所有输出结果:

代码

2010-10-19 11:30:28.910 Test[1293:207] 1:aMyobject:1

1 2  下一页

Tags:关于 Ojbective self

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接