成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

iOS何時使用self.

移動開發 iOS
在ObjC的學習中經常會碰到是否應該使用self的苦惱,或者說什么時候使用全局變量,什么時候self ?

大多數的答案是:“這與objc的存取方法有關”

怎么樣才能有關呢?接下來通過幾個小例子來看一下。

首先我們創建一個學生類:Student類

這個學生類里有學生的id和學生的姓名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. 學生類的實現文件  
  22.  
  23. #import  
  24. "Student.h"  
  25.  
  26. @implementation  
  27. Student   
  28.  
  29. @synthesize  
  30. id,name;  
  31.  
  32. @end  

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

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

setName:@“eve”]; 其中student是Student類的對象,如果在Student類內部訪問其成員屬性使用[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也有一定的變化。

如何這個時候使用self.name編譯器就會報錯,這樣就說明了我們通常使用self.name實際使用的是student類name的get方法,同理name的set方法亦是如此。

另外網絡上也有人從內存管理方面來說明的,我將其剪切出來以供學習:

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方法中創建一個Student類的對象  
  58.  
  59. Student  
  60. *mystudent = [[Student alloc] init];  
  61. self.student  
  62. = mystudent;  
  63. [mystudent  
  64. release];  

接下來就需要從內存角度來分析它們之間的區別了:

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指對象引用計數,student的property  
  9. 是retain 默認使用self.student引用計數+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 對象內存已釋放,如果調用,會有異常  

3、加self直接賦值方式

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

2;容易造成內存泄露

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

個人總結:只需要在屬性初始化的時候使用self.屬性,其他時候直接使用屬性名就行;使用self.是 使retaincount+1,為了確保當前類對此屬性具有擁有權

個人使用習慣:

  1. @interface CustomClass : UIViewController 
  2.     NSString *str 
  3. @property (retain, nonatomic) NSString *str; @implementation CustomClass @synthesize str; -(void)viewDidLoad 
  4. //方法一  用alloc必須手動釋放一次 self.str  =  [[NSString alloc]initWithString:@"my str"]; 
  5.      [str release]; //方法二 用類方法不用 self.str =     [NSString stringWithString:@"my str"]; 
  6.  
  7.     以后調用時直接使用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. }  
責任編輯:張葉青 來源: 開源社區
相關推薦

2011-07-20 13:34:37

Objective-C self.

2012-01-18 10:13:50

Objective-CiOSself

2020-10-21 14:54:02

RustGolang開發

2011-08-08 15:43:01

MySQL索引

2021-04-12 07:34:03

Java集合框架

2021-11-26 09:00:00

數據庫數據集工具

2019-11-29 07:53:07

DNSTCP網絡協議

2020-12-13 14:32:22

5GWi-Fi 6

2021-12-09 09:52:36

云原生安全工具云安全

2012-02-08 11:01:53

HibernateJava

2024-04-16 12:00:14

API系統

2024-08-01 10:10:24

MySQL場景搜索

2020-10-10 10:20:11

云計算云安全技術

2025-05-19 08:13:45

2021-04-25 15:06:16

微軟虛擬桌面IT

2021-06-01 11:11:26

物聯網互聯網IoT

2023-11-10 12:55:00

消息代理事件代理

2021-04-14 07:52:00

Vue 作用域插槽

2020-02-25 16:00:28

JavaScript數據技術

2018-12-12 09:59:47

微服務架構分布式系統
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 免费成人高清在线视频 | 国产yw851.c免费观看网站 | 一区二区三区欧美 | 欧美精品1区2区3区 免费黄篇 | 国产精品久久国产精品 | 日韩欧美一区二区三区免费观看 | 日日骚网 | 午夜精品久久久久久久久久久久久 | 性色av一区二区三区 | 成年网站在线观看 | 97人人爱 | 麻豆毛片 | 在线看一区二区三区 | 免费观看的黄色网址 | av网站免费看 | 亚洲视频免费在线播放 | 一级高清 | 欧美一区 | 国产一区二区在线播放视频 | 亚洲国产欧美国产综合一区 | 99re在线观看 | 国产精品久久久久久久久久久免费看 | 免费国产视频在线观看 | 最新国产精品精品视频 | 波多野结衣二区 | 激情 一区 | 黄a大片 | 在线一区 | 精品免费视频 | 亚洲成人精品一区 | 免费a v网站 | 亚洲伊人精品酒店 | 欧美a免费| av一级久久 | 欧美一卡二卡在线 | 国产午夜精品理论片a大结局 | 成人av免费| 国产精品中文字幕在线观看 | 欧美一级黄视频 | 久久久久久国产精品免费免费狐狸 | 久久不卡区 |