您的位置:首页 > 职场人生

黑马程序员——oc之self的使用

2015-09-10 23:41 543 查看
 -------android培训java培训iOS培训.Net培训、期待与您交流!
----------

首先我们创建一个学生类:Student类

这个学生类里有学生的id和学生的姓名name
#import  
 
 
@interface  
Student : NSObject{  
 
//idname  
 
NSString *id;  
 
NSString *name;  
}  
 
@property  
(nonatomic,strong) NSString *id;  
@property  
(nonatomic,strong) NSString *name;  
 
@end  
 
学生类的实现文件  
 
#import  
"Student.h"  
 
@implementation  
Student   
147c9

 
@synthesize  
id,name;  
 
@end  

如果使用上面的方法来定义学生类的属性的get、set方法的时候,那么其他类访问的时候就是:

获取student的名字通过student.name来获取,给名字赋值则使用[student

setName:@“eve”]; 其中student是Student类的对象,如果在Student类内部访问其成员属性使用[self 

setName:@”evo”], 访问使用self.name;

上面的方法只是一种,但是很难解释self该不该使用。请看下面:

我们改写Student类
#import  
 
 
@interface  
Student : NSObject{  
 
//idname  
 
NSString *_id;  
 
NSString *_name;  
}  
 
@property  
(nonatomic,strong) NSString *id;  
@property  
(nonatomic,strong) NSString *name;  
 
@end  
 
.m文件  
 
#import  
"Student.h"  
 
@implementation  
Student   
 
@synthesize  
id = _id;  
@synthesize  
name = _name;  
 
@end  

可见这样的写法我们增加了_id和_name,其中@synthesize也有一定的变化。

如何这个时候使用self.name编译器就会报错,这样就说明了我们通常使用self.name实际使用的是student类name的get方法,同理name的set方法亦是如此。

另外网络上也有人从内存管理方面来说明的,我将其剪切出来以供学习:

ViewController.h文件,使用Student类,代码如下:
#import  
@  
class Student;  
 
@  
interface ViewController : UIViewController{  
 
Student *_student;  
}  
 
@property  
(nonatomic, retain) Student *student;  
 
@end  
 
ViewController.m文件,代码:  
 
#import  
"ViewController.h"  
#import  
"Student.h"  
 
@implementation  
ViewController  
@synthesize  
student = _student;  
 
-  
(void)didReceiveMemoryWarning  
{  
 
[super didReceiveMemoryWarning];  
}  
 
#pragma  
mark - View lifecycle  
 
-  
(void)viewDidLoad  
{  
 
[super viewDidLoad];  
}  
 
-  
(void) dealloc  
{  
 
[_student release];  
 
_student = nil;  
 
[super dealloc];  
}  
其它的方法没有使用到,所以这里就不在显示了。  
 
在ViewController.m的viewDidLoad方法中创建一个Student类的对象  
 
Student  
*mystudent = [[Student alloc] init];  
self.student  
= mystudent;  
[mystudent  
release];  

接下来就需要从内存角度来分析它们之间的区别了:

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才会释放该内存

个人总结:只需要在属性初始化的时候使用self.属性,其他时候直接使用属性名就行;使用self.是 使retaincount+1,为了确保当前类对此属性具有拥有权
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: