IOS開發(49)之關於 self與記憶體相關的用法總結

來源:互聯網
上載者:User

今天新同事問了一些關於什麼時候用全域變數,什麼時候用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才會釋放該記憶體。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.