iOS何時(shí)使用self.
大多數(shù)的答案是:“這與objc的存取方法有關(guān)”
怎么樣才能有關(guān)呢?接下來(lái)通過(guò)幾個(gè)小例子來(lái)看一下。
首先我們創(chuàng)建一個(gè)學(xué)生類:Student類
這個(gè)學(xué)生類里有學(xué)生的id和學(xué)生的姓名name
- #import
 - @interface
 - Student : NSObject{
 - //idname
 - NSString *id;
 - NSString *name;
 - }
 - @property
 - (nonatomic,strong) NSString *id;
 - @property
 - (nonatomic,strong) NSString *name;
 - @end
 - 學(xué)生類的實(shí)現(xiàn)文件
 - #import
 - "Student.h"
 - @implementation
 - Student
 - @synthesize
 - id,name;
 - @end
 
如果使用上面的方法來(lái)定義學(xué)生類的屬性的get、set方法的時(shí)候,那么其他類訪問(wèn)的時(shí)候就是:
獲取student的名字通過(guò)student.name來(lái)獲取,給名字賦值則使用[student
setName:@“eve”]; 其中student是Student類的對(duì)象,如果在Student類內(nèi)部訪問(wèn)其成員屬性使用[self  
setName:@”evo”], 訪問(wèn)使用self.name;
上面的方法只是一種,但是很難解釋self該不該使用。請(qǐng)看下面:
我們改寫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
 
可見(jiàn)這樣的寫法我們?cè)黾恿薩id和_name,其中@synthesize也有一定的變化。
如何這個(gè)時(shí)候使用self.name編譯器就會(huì)報(bào)錯(cuò),這樣就說(shuō)明了我們通常使用self.name實(shí)際使用的是student類name的get方法,同理name的set方法亦是如此。
另外網(wǎng)絡(luò)上也有人從內(nèi)存管理方面來(lái)說(shuō)明的,我將其剪切出來(lái)以供學(xué)習(xí):
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];
 - }
 - 其它的方法沒(méi)有使用到,所以這里就不在顯示了。
 - 在ViewController.m的viewDidLoad方法中創(chuàng)建一個(gè)Student類的對(duì)象
 - Student
 - *mystudent = [[Student alloc] init];
 - self.student
 - = mystudent;
 - [mystudent
 - release];
 
接下來(lái)就需要從內(nèi)存角度來(lái)分析它們之間的區(qū)別了:
1、加self的方式:
- Student
 - *mystudent = [[Student alloc] init]; //mystudent 對(duì)象
 - retainCount = 1;
 - self.student
 - = mystudent; //student 對(duì)象 retainCount = 2;
 - [mystudent
 - release];//student 對(duì)象 retainCount = 1;
 - retainCount指對(duì)象引用計(jì)數(shù),student的property
 - 是retain 默認(rèn)使用self.student引用計(jì)數(shù)+1。
 
2、不加self的方式
- Student
 - *mystudent = [[Student alloc] init]; //mystudent 對(duì)象
 - retainCount = 1;
 - student
 - = mystudent; //student 對(duì)象 retainCount = 1;
 - [mystudent
 - release]; //student 對(duì)象內(nèi)存已釋放,如果調(diào)用,會(huì)有異常
 
3、加self直接賦值方式
self.student = [[Student alloc] init];//student 對(duì)象 retainCount =
2;容易造成內(nèi)存泄露
由于objective-c內(nèi)存管理是根據(jù)引用計(jì)數(shù)處理的,當(dāng)一個(gè)對(duì)象的引用計(jì)數(shù)為零時(shí),gcc才會(huì)釋放該內(nèi)存
個(gè)人總結(jié):只需要在屬性初始化的時(shí)候使用self.屬性,其他時(shí)候直接使用屬性名就行;使用self.是 使retaincount+1,為了確保當(dāng)前類對(duì)此屬性具有擁有權(quán)
個(gè)人使用習(xí)慣:
- @interface CustomClass : UIViewController
 - {
 - NSString *str
 - }
 - @property (retain, nonatomic) NSString *str; @implementation CustomClass @synthesize str; -(void)viewDidLoad
 - { //方法一 用alloc必須手動(dòng)釋放一次 self.str = [[NSString alloc]initWithString:@"my str"];
 - [str release]; //方法二 用類方法不用 self.str = [NSString stringWithString:@"my str"];
 - 以后調(diào)用時(shí)直接使用str,不必使用self.str
 - [str appendString:@"\n"];
 - } //在dealloc中必須釋放 - (void)dealloc
 - { //方法一 [str release];
 - str = nil; //方法二 self.str = nil;
 - [super dealloc];
 - }
 















 
 
 






 
 
 
 