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

iPhone開發(fā)之屬性變量如何Release

移動開發(fā) iOS
iPhone開發(fā)之屬性變量如何Release是本文要介紹的內(nèi)容,主要是為了釋放對象擁有的實(shí)例變量,常用的方法是在dealloc中調(diào)用release,來看詳細(xì)內(nèi)容。

iPhone開發(fā)之屬性變量如何Release是本文要介紹的內(nèi)容,主要是為了釋放對象擁有的實(shí)例變量,常用的方法是在dealloc中調(diào)用release,比如下面的代碼:

  1. @interface MyClass : NSObject {    
  2. NSString *name;  
  3. }    
  4. @end   
  5. @implementation MyClass//something...  
  6. - (void)dealloc{    
  7. [name release];    
  8. [super dealloc];  
  9. }   
  10. @end 

如果這里的name是屬性變量呢?在IPhone開發(fā)基礎(chǔ)教程這本書里,你會經(jīng)常在看到這樣的代碼:

  1. @interface MyClass : NSObject {    
  2. NSString *name;  
  3. }  
  4.  @property(retain) NSString *name;   
  5.  @end   
  6.  @implementation MyClass @synthesize name;  
  7.   - (void)dealloc{  self.setName = nil;    
  8.   [super dealloc];  
  9.   }   
  10. @end 

這里并沒有直接訪問變量本身,而是使用了編譯器自動生成的setter。那問題就來了,咋的一賦值成nil,就能release了?想想看一般的setter的是怎么寫的,看下面:

  1. - (void) setName:(NSString *)   
  2. value {     
  3. [value retain];   
  4. // calls [nil retain], which does nothing   [name release];   
  5. // releases the backing variable (ivar)   name = value;     
  6. // sets the backing variable (ivar) to nil} 

ok,很方便的寫法。但是不是說就沒有問題呢?請參考下面的討論,在KVC機(jī)制中是會有問題的。

  1. http://stackoverflow.com/questions/192721/why-shouldnt-i-use-objective-c-2-0-accessors-in-init-dealloc  
  2.  
  3. http://stackoverflow.com/questions/1283419/valid-use-of-accessors-in-init-and-dealloc-methods 

小結(jié):iPhone開發(fā)之屬性變量如何Release的內(nèi)容介紹完了,希望通過本文的學(xué)習(xí)能對你有所幫助!

責(zé)任編輯:zhaolei 來源: 博客園
相關(guān)推薦

2011-08-10 10:10:21

iPhoneUIPopoverCo

2011-08-01 18:27:58

iPhone開發(fā) UISearchBa

2011-08-10 09:50:43

iPhoneArchive數(shù)據(jù)

2011-08-08 13:57:19

iPhone開發(fā) 打包 DEB

2011-08-11 10:03:43

iPhonecocoaNSRunLoop

2011-08-12 11:31:46

iPhoneUIView動畫

2013-07-23 07:34:54

iOS開發(fā)學(xué)習(xí)適配iphone5

2011-08-22 13:46:15

iPhone開發(fā)GameKit 藍(lán)牙

2011-08-09 11:36:41

iPhoneUIPickerVieDEMO

2011-07-18 14:39:53

iPhone SDK UIKit

2011-07-27 09:33:14

iPhone 網(wǎng)絡(luò) Web

2011-08-11 17:15:54

iPhone歸檔

2011-06-07 17:16:47

iPhone 數(shù)據(jù)

2011-08-18 10:02:47

iPhone SDKOpenFlow

2011-08-18 09:52:13

iPhone SDKUIPageContr

2011-08-18 10:59:57

iPhone開發(fā)消息通信NSNotificat

2011-08-17 15:19:38

iPhone應(yīng)用數(shù)據(jù)

2013-07-25 15:15:26

iOS開發(fā)學(xué)習(xí)iOS全局變量

2011-07-07 15:45:45

iPhone SQLite 數(shù)據(jù)

2011-08-03 16:01:24

iPhone應(yīng)用開發(fā) 自動登陸
點(diǎn)贊
收藏

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