自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

iOS何時(shí)使用self.

移動(dòng)開發(fā) iOS
在ObjC的學(xué)習(xí)中經(jīng)常會(huì)碰到是否應(yīng)該使用self的苦惱,或者說什么時(shí)候使用全局變量,什么時(shí)候self ?

大多數(shù)的答案是:“這與objc的存取方法有關(guān)”

怎么樣才能有關(guān)呢?接下來通過幾個(gè)小例子來看一下。

首先我們創(chuàng)建一個(gè)學(xué)生類:Student類

這個(gè)學(xué)生類里有學(xué)生的id和學(xué)生的姓名name

  1. #import  
  2.  
  3.  
  4. @interface  
  5. Student : NSObject{  
  6.  
  7. //idname  
  8.  
  9. NSString *id;  
  10.  
  11. NSString *name;  
  12. }  
  13.  
  14. @property  
  15. (nonatomic,strong) NSString *id;  
  16. @property  
  17. (nonatomic,strong) NSString *name;  
  18.  
  19. @end  
  20.  
  21. 學(xué)生類的實(shí)現(xiàn)文件  
  22.  
  23. #import  
  24. "Student.h"  
  25.  
  26. @implementation  
  27. Student   
  28.  
  29. @synthesize  
  30. id,name;  
  31.  
  32. @end  

如果使用上面的方法來定義學(xué)生類的屬性的get、set方法的時(shí)候,那么其他類訪問的時(shí)候就是:

獲取student的名字通過student.name來獲取,給名字賦值則使用[student

setName:@“eve”]; 其中student是Student類的對象,如果在Student類內(nèi)部訪問其成員屬性使用[self
setName:@”evo”], 訪問使用self.name;

上面的方法只是一種,但是很難解釋self該不該使用。請看下面:

我們改寫Student類

  1. #import  
  2.  
  3.  
  4. @interface  
  5. Student : NSObject{  
  6.  
  7. //idname  
  8.  
  9. NSString *_id;  
  10.  
  11. NSString *_name;  
  12. }  
  13.  
  14. @property  
  15. (nonatomic,strong) NSString *id;  
  16. @property  
  17. (nonatomic,strong) NSString *name;  
  18.  
  19. @end  
  20.  
  21. .m文件  
  22.  
  23. #import  
  24. "Student.h"  
  25.  
  26. @implementation  
  27. Student   
  28.  
  29. @synthesize  
  30. id = _id;  
  31. @synthesize  
  32. name = _name;  
  33.  
  34. @end  

可見這樣的寫法我們增加了_id和_name,其中@synthesize也有一定的變化。

如何這個(gè)時(shí)候使用self.name編譯器就會(huì)報(bào)錯(cuò),這樣就說明了我們通常使用self.name實(shí)際使用的是student類name的get方法,同理name的set方法亦是如此。

另外網(wǎng)絡(luò)上也有人從內(nèi)存管理方面來說明的,我將其剪切出來以供學(xué)習(xí):

ViewController.h文件,使用Student類,代碼如下:

  1. #import  
  2. @  
  3. class Student;  
  4.  
  5. @  
  6. interface ViewController : UIViewController{  
  7.  
  8. Student *_student;  
  9. }  
  10.  
  11. @property  
  12. (nonatomic, retain) Student *student;  
  13.  
  14. @end  
  15.  
  16. ViewController.m文件,代碼:  
  17.  
  18. #import  
  19. "ViewController.h"  
  20. #import  
  21. "Student.h"  
  22.  
  23. @implementation  
  24. ViewController  
  25. @synthesize  
  26. student = _student;  
  27.  
  28. -  
  29. (void)didReceiveMemoryWarning  
  30. {  
  31.  
  32. [super didReceiveMemoryWarning];  
  33. }  
  34.  
  35. #pragma  
  36. mark - View lifecycle  
  37.  
  38. -  
  39. (void)viewDidLoad  
  40. {  
  41.  
  42. [super viewDidLoad];  
  43. }  
  44.  
  45. -  
  46. (void) dealloc  
  47. {  
  48.  
  49. [_student release];  
  50.  
  51. _student = nil;  
  52.  
  53. [super dealloc];  
  54. }  
  55. 其它的方法沒有使用到,所以這里就不在顯示了。  
  56.  
  57. 在ViewController.m的viewDidLoad方法中創(chuàng)建一個(gè)Student類的對象  
  58.  
  59. Student  
  60. *mystudent = [[Student alloc] init];  
  61. self.student  
  62. = mystudent;  
  63. [mystudent  
  64. release];  

接下來就需要從內(nèi)存角度來分析它們之間的區(qū)別了:

1、加self的方式:

  1. Student  
  2. *mystudent = [[Student alloc] init];  //mystudent 對象  
  3. retainCount = 1;  
  4. self.student  
  5. = mystudent; //student 對象 retainCount = 2;  
  6. [mystudent  
  7. release];//student 對象 retainCount = 1;  
  8. retainCount指對象引用計(jì)數(shù),student的property  
  9. 是retain 默認(rèn)使用self.student引用計(jì)數(shù)+1。  

2、不加self的方式

  1. Student  
  2. *mystudent = [[Student alloc] init];  //mystudent 對象  
  3. retainCount = 1;  
  4. student  
  5. = mystudent; //student 對象 retainCount = 1;  
  6. [mystudent  
  7. release]; //student 對象內(nèi)存已釋放,如果調(diào)用,會(huì)有異常  

3、加self直接賦值方式

self.student = [[Student alloc] init];//student 對象 retainCount =

2;容易造成內(nèi)存泄露

由于objective-c內(nèi)存管理是根據(jù)引用計(jì)數(shù)處理的,當(dāng)一個(gè)對象的引用計(jì)數(shù)為零時(shí),gcc才會(huì)釋放該內(nèi)存

個(gè)人總結(jié):只需要在屬性初始化的時(shí)候使用self.屬性,其他時(shí)候直接使用屬性名就行;使用self.是 使retaincount+1,為了確保當(dāng)前類對此屬性具有擁有權(quán)

個(gè)人使用習(xí)慣:

  1. @interface CustomClass : UIViewController 
  2.     NSString *str 
  3. @property (retain, nonatomic) NSString *str; @implementation CustomClass @synthesize str; -(void)viewDidLoad 
  4. //方法一  用alloc必須手動(dòng)釋放一次 self.str  =  [[NSString alloc]initWithString:@"my str"]; 
  5.      [str release]; //方法二 用類方法不用 self.str =     [NSString stringWithString:@"my str"]; 
  6.  
  7.     以后調(diào)用時(shí)直接使用str,不必使用self.str 
  8.    [str appendString:@"\n"]; 
  9. //在dealloc中必須釋放 - (void)dealloc 
  10. //方法一  [str release]; 
  11.     str = nil; //方法二 self.str = nil; 
  12.  
  13.     [super dealloc]; 
  14. }  
責(zé)任編輯:張葉青 來源: 開源社區(qū)
相關(guān)推薦

2011-07-20 13:34:37

Objective-C self.

2012-01-18 10:13:50

Objective-CiOSself

2020-10-21 14:54:02

RustGolang開發(fā)

2021-11-26 09:00:00

數(shù)據(jù)庫數(shù)據(jù)集工具

2021-04-12 07:34:03

Java集合框架

2011-08-08 15:43:01

MySQL索引

2019-11-29 07:53:07

DNSTCP網(wǎng)絡(luò)協(xié)議

2021-12-09 09:52:36

云原生安全工具云安全

2020-12-13 14:32:22

5GWi-Fi 6

2012-02-08 11:01:53

HibernateJava

2024-08-01 10:10:24

MySQL場景搜索

2024-04-16 12:00:14

API系統(tǒng)

2020-10-10 10:20:11

云計(jì)算云安全技術(shù)

2021-04-25 15:06:16

微軟虛擬桌面IT

2021-06-01 11:11:26

物聯(lián)網(wǎng)互聯(lián)網(wǎng)IoT

2024-10-07 08:49:25

2018-12-12 09:59:47

微服務(wù)架構(gòu)分布式系統(tǒng)

2020-02-25 16:00:28

JavaScript數(shù)據(jù)技術(shù)

2023-11-10 12:55:00

消息代理事件代理

2021-04-14 07:52:00

Vue 作用域插槽
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)