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

蘋果開發(fā)教程 Cocoa內存管理筆記

移動開發(fā) iOS
本文介紹的是蘋果開發(fā)教程 Cocoa內存管理筆記,很詳細的介紹了對內存的管理,如何管理,我們來看內容。

蘋果開發(fā)教程 Cocoa內存管理筆記是本文要介紹的內容,內容分為兩種方式進行介紹,我們來看詳細內容。

下面的這種方式是不對的

  1. Instance you don’t own is sent release- (void)reset {NSNumber *zero = [NSNumber numberWithInteger:0]; 

創(chuàng)建的是一個autorelease的對象[self setCount:zero];[zero release];//這里釋放是危險的}

  1. When you add an object to a collection such as an array, dictionary, or set, the collection takes ownership of 

it.在集合中增加object,那么這個object的所有者就變成了集合了

代碼

  1. // ...for (i = 0; i < 10; i++) {NSNumber *convenienceNumber = [NSNumber numberWithInteger:i];  
  2. [array addObject:convenienceNumber];  
  3. }  
  4. //這種情況不需要releaseNSMutableArray *array;NSUInteger i;  
  5. // ...for (i = 0; i < 10; i++) {NSNumber *allocedNumber = [[NSNumber alloc] initWithInteger: i]  
  6. ;[array addObject:allocedNumber];[allocedNumber release];}  
  7. //這種情況需要,此處只是將retain的計數(shù)減1而已 

安全返回對象

下面兩種方式是正確的

  1. (NSString *)fullName {    
  2.  NSString *string = [NSString stringWithFormat:@"%@ %@", firstName, lastName];    
  3.  return string;    
  4.  }    
  5.      
  6.  (NSString *)fullName {    
  7.  NSString *string = [[[NSString alloc] initWithFormat:@"%@ %@", firstName,    
  8.  lastName] autorelease];    
  9.  return string;    
  10.  }  

相反,下面的方式是錯誤的

  1.  (NSString *)fullName {    
  2.  NSString *string = [[[NSString alloc] initWithFormat:@"%@ %@", firstName,    
  3.  lastName] release];    
  4.  return string;    
  5. }   

8 同樣,下面的方式也是錯的

  1. (NSString *)fullName {    
  2. NSString *string = [[NSString alloc] initWithFormat:@"%@ %@", firstName,    
  3. lastName];    
  4. return string;    
  5. }   

對象拷貝機制

有兩種實現(xiàn)拷貝協(xié)議的copyWithZone:方法的方式:

使用alloc and init..

使用 NSCopyObject. 

看下面對象的定義

  1. @interface Product : NSObject <NSCopying> 
  2. {  
  3. NSString *productName;  
  4. float price;  
  5. id delegate;  
  6. }  
  7. @end 

拷貝后的內存位置圖如下:

假設從supercalass繼承了NSCopying,但是父類沒有實現(xiàn)NSCopying,那么你要實現(xiàn)的話必須拷貝super的實例,同樣包括自己聲明的變量。一般情況下安全的方式是使用如alloc,
init..., and set methods

另外一方面,如果super類已經實現(xiàn)了NSCopying,并且在你的類中你聲明了一些實例變量,那么你必須實現(xiàn)copyWithZone:

如果類沒有繼承NSCopying的行為,那么實現(xiàn)copyWithZone: using alloc,init..., and set methods.下面是一個例子

  1. - (id)copyWithZone:(NSZone *)zone  
  2. {  
  3. Product *copy = [[[self class] allocWithZone: zone]  
  4. initWithProductName:[self productName]  
  5. price:[self price]];  
  6. [copy setDelegate:[self delegate]];  
  7. return copy;  

有些繼承了NSCopying behavior的類,但是他們的super類的實現(xiàn)可能使用了 NSCopyObject function. NSCopyObject creates an exact shallow copy of an object

by copying instance variable values but not the data they point to. 舉個例子, NSCell類采用如下的方式實現(xiàn)copyWithZone

  1. - (id)copyWithZone:(NSZone *)zone  
  2. {  
  3. NSCell *cellCopy = NSCopyObject(self, 0, zone);  
  4. /* Assume that other initialization takes place here. */  
  5. cellCopy->image = nil;  
  6. [cellCopy setImage:[self image]];  
  7. return cellCopy;  

在上面的實現(xiàn)采用的是淺拷貝

對可變長度的對象的拷貝實現(xiàn) ,要繼承NSMutableCopying

Core Foundation Objects in Cocoa中的內存管理

  1. Core Foundation's memory allocation policy is that you need to release values returned   
  2. by functions with “Copy” or “Create” in their name; you should not release values   
  3. returned by functions that do not have “Copy” or “Create” in their name. 

舉幾個例子

  1. NSString *str = [[NSString alloc] initWithCharacters: ...]; ... [str release];  
  2. is equivalent to  
  3. CFStringRef str = CFStringCreateWithCharacters(...); ...  
  4. CFRelease(str);  
  5. and  
  6. NSString *str = (NSString *)CFStringCreateWithCharacters(...); ...  
  7. [str release];  
  8. and  
  9. NSString *str = (NSString *)CFStringCreateWithCharacters(...);  
  10. ... [str autorelease];  
  11. Memory Management of Nib Objects 

The File’s Owner of a nib file缺省要去釋放NIB資源及頂層的對象

NIB文件的全局擁有者是全局應用對象NSApp,但是當Cocoa應用終止時,nib中的頂層對象也沒有自動獲得dealloc消息,因為NSApp已經被析構了。換句話說,即使nib主文件中,你也不得不管理頂層對象的內存

實際上也不用擔心,mac已經有兩個特征可以幫助你了

NSWindow對象有一個isReleasedWhenClosed屬性,設置為YES則關閉窗口對象時自動關閉相關對象

nib文件的擁有者是一個NSWindowController對象,那么他會調用NSDocument來管理一個NSWindowController的實例,會自動釋放他管理的窗口的

所以現(xiàn)實情況就是雖然你要負責釋放一個nib文件中的top-level對象,但是只要你的nib文件的owner是一個NSWindowController的實例,那么它會幫你釋放的。如果你的一個對象加載了nib自身并且文件的擁有者并不是NSWindowController,那么你可以為nib中的對象定義outlets,這樣你就可以在恰當?shù)臅r候釋放他們。如果你不想為每個對象都聲明outlet,你也可以這樣:

NSNib類的instantiateNibWithOwner:topLevelObjects: 方法來獲得nib文件中的所有頂層對象

內存管理總之可以歸結為:

(1)你通過帶alloc,new,copy的函數(shù)創(chuàng)建的對象,你擁有他

(2)通過retain你可以獲得擁有權

(3)任何一個對象都可能有很多個owner

(4)你擁有的對象你必須通過發(fā)送release或者是autorelease釋放他們

(5)你不能釋放不是你擁有的對象

(6)對set類型的賦值函數(shù),你可以retain傳入的對象,你也可以copy一份,看你自己的要求咯

(7)在函數(shù)(void)dealloc中一定要釋放你聲明的instance變量呀

(8)指針變量使用完了一定要設為nil

(9)你要確定一個對象不被釋放掉,你***提前retain一下

(10)在任何時候都不要直接調用dealloc

小結:蘋果開發(fā)教程 Cocoa內存管理筆記的內容介紹完了,希望本文對你有所幫助!

責任編輯:zhaolei 來源: 互聯(lián)網
相關推薦

2011-06-17 15:57:46

CocoaXcode蘋果

2011-07-28 11:12:25

Cocoa 內存

2011-08-15 16:28:06

Cocoa內存管理

2011-07-07 09:54:01

Cocoa Core Foundation

2011-08-10 18:37:32

CocoaMac OS X

2011-06-17 16:23:49

Cocoa蘋果

2011-06-15 17:02:02

CocoaiOS

2011-07-25 13:15:34

Cocoa MVC 架構

2011-07-26 15:29:36

Cocoa 模式

2011-05-11 17:48:31

CocoaiOS

2011-07-26 10:42:00

Cocoa Cocoa2d 游戲

2011-06-15 16:11:51

UIKitCocoa TouchiOS

2011-06-17 15:38:15

Cocoa蘋果

2014-07-31 10:48:09

Android內存管理OOM

2011-07-21 14:42:45

iOS UIViewCont 內存

2011-07-25 14:32:40

Cocoa 框架 函數(shù)

2011-08-11 15:46:55

CocoaCocoa Touch框架

2011-08-15 16:09:44

Cocoa對象Objective-C

2011-07-07 14:46:10

Cocoa Xcode

2011-08-15 15:26:20

iPhone開發(fā)CocoaXML
點贊
收藏

51CTO技術棧公眾號