天天看點

objective-c self

什麼時候用self.指派的問題,在此說明一下。

何時使用self.在網上搜尋或者論壇裡的回複大多都是簡簡單單的說這與objc的存取方法有關,如何與存取方式有關究竟他們之間的是什麼樣的關系就很少有同學回答了。下面以代碼來說明問題:

建立一個Student類,繼承NSObject類,代碼: 

01

#import <Foundation/Foundation.h>

02

03

@ interface Student : NSObject{

04

05

NSString *idd;

06

NSString *name;

07

}

08

@property (nonatomic, retain) NSString *idd;

09

@property (nonatomic, retain) NSString *name;

10

11

@end

.m檔案 代碼:

01

#import "Student.h"

02

03

@implementation Student

04

@synthesize idd,name;

05

06

- (

void

)dealloc

07

{

08

[idd release];

09

[name release];

10

[super dealloc];

11

}

12

13

14

@end

使用@propety @synthesize實作Student的成員屬性的set get方法。通常我們在其他類裡通路Student的成員屬性的做法:

擷取student的名字通過student.name,給名字指派[student setName:@“jordy”]; 其中student是Student類對象,如果在Student類内部通路其成員屬性使用[self setName:@”jordy”], 通路使用self.name;

注意:上述的代碼,由于wordpress的原因,代碼中的字元會自動儲存為中文格式。你在使用時記得改為英文格式。

在Student.h和Student.m檔案,是我們習慣性的寫法,但似乎還是不能解釋什麼加self和不加self的差別,請看下面代碼,是另一種習慣性的寫法,還以Student類為例:

.h檔案 代碼: 

01

#import <Foundation/Foundation.h>

02

03

@ interface Student : NSObject{

04

05

NSString *_idd;

06

NSString *_name;

07

}

08

@property (nonatomic, retain) NSString *idd;

09

@property (nonatomic, retain) NSString *name;

10

11

@end

 .m檔案 代碼:

01

#import "Student.h"

02

03

@implementation Student

04

@synthesize idd = _idd;

05

@synthesize name = _name;

06

07

- (

void

)dealloc

08

{

09

[_idd release];

10

_idd = nil;

11

[_name release];

12

_name = nil;

13

[super dealloc];

14

}

15

16

17

@end

可以注意到上述代碼,與之前的代碼,在.h檔案name變量改寫為了_name;在.m檔案中@sythesize的寫法也發生了變化。

如果通過方法self._name擷取屬性的值,xcode編譯器會提示錯誤,其實這也就說明了,我們通常使用self.name實際使用的是student類name的get方法,同理name的set方法亦是如此。

接下來從記憶體管理來說明使用self.和不使用self的差別:

ViewController.h檔案,使用Student類,代碼如下:

01

#import <UIKit/UIKit.h>

02

class

Student;

03

04

@ interface ViewController : UIViewController{

05

06

Student *_student;

07

}

08

09

@property (nonatomic, retain) Student *student;

10

11

@end

ViewController.m檔案,代碼:

01

#import "ViewController.h"

02

#import "Student.h"

03

04

@implementation ViewController

05

@synthesize student = _student;

06

07

- (

void

)didReceiveMemoryWarning

08

{

09

[super didReceiveMemoryWarning];

10

}

11

12

#pragma mark - View lifecycle

13

14

- (

void

)viewDidLoad

15

{

16

[super viewDidLoad];

17

}

18

19

- (

void

) dealloc

20

{

21

[_student release];

22

_student = nil;

23

[super dealloc];

24

}

其它的方法沒有使用到,是以這裡就不在顯示了。

在ViewController.m的viewDidLoad方法中建立一個Student類的對象

1

Student *mystudent = [[Student alloc] init];

2

self.student = mystudent;

3

[mystudent release];

這是相信有人會有疑問了,問什麼建立student對象要這麼複雜,似乎直接使用self.student = [[Student alloc] init]; 也沒有問題,不加self有時也是挺正常的呀?

接下來就需要從記憶體角度來分析它們之間的差別了:

1、加self的方式: 

1

Student *mystudent = [[Student alloc] init];       

//mystudent 對象 retainCount = 1;

2

self.student = mystudent;   

//student 對象 retainCount = 2;

3

[mystudent release];  

//student 對象 retainCount = 1;

retainCount指對象引用計數,student的property 是retain 預設使用self.student引用計數+1。

2、不加self的方式

1

Student *mystudent = [[Student alloc] init];       

//mystudent 對象 retainCount = 1;

2

student = mystudent;   

//student 對象 retainCount = 1;

3

[mystudent release];   

//student 對象記憶體已釋放,如果調用,會有異常

3、加self直接指派方式

1

self.student = [[Student alloc] init];  

//student 對象 retainCount = 2;容易造成記憶體洩露

由于objective-c記憶體管理是根據引用計數處理的,當一個對象的引用計數為零時,gcc才會釋放該記憶體。

繼續閱讀