Objective-C:MRC(引用计数器)获得对象所有权的方式(init、retain、copy等)
生活随笔
收集整理的這篇文章主要介紹了
Objective-C:MRC(引用计数器)获得对象所有权的方式(init、retain、copy等)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
? ???.h聲明文件
1 // Integer.h 2 // 02-MRC 3 // 4 // Created by ma c on 15/8/13. 5 // Copyright (c) 2015年. All rights reserved. 6 // 7 8 #import <Foundation/Foundation.h> 9 10 @interface Integer : NSObject 11 @property(nonatomic,assign)NSInteger i; 12 -(id)initWithI:(NSInteger) i; 13 -(void) print; 14 +(Integer *)integerWithIntger:(NSInteger) i; 15 @end?
.m實(shí)現(xiàn)文件
1 // Integer.m 2 // 02-MRC 3 // 4 // Created by ma c on 15/8/13. 5 // Copyright (c) 2015年. All rights reserved. 6 // 7 8 #import "Integer.h" 9 10 @implementation Integer 11 -(id)initWithI:(NSInteger) i 12 { 13 self = [super init]; 14 if(self) 15 { 16 _i = i; 17 } 18 return self; 19 } 20 +(Integer *)integerWithIntger:(NSInteger) i 21 { 22 return [[Integer alloc]initWithI:i]; 23 } 24 25 -(void) print 26 { 27 NSLog(@"i = %ld",_i); 28 } 29 -(void)dealloc 30 { 31 NSLog(@"integer dealloc"); 32 [super dealloc]; 33 } 34 @end
主函數(shù)測(cè)試
1 // main.m 2 // 02-MRC 3 // 4 // Created by ma c on 15/8/13. 5 // Copyright (c) 2015年. All rights reserved. 6 // 7 8 #import <Foundation/Foundation.h> 9 #import "Integer.h" 10 int main(int argc, const char * argv[]) 11 { 12 @autoreleasepool 13 { 14 //測(cè)試手動(dòng)引用計(jì)數(shù) 15 //1.創(chuàng)建對(duì)象會(huì)獲得對(duì)象所有權(quán) 16 Integer *i1 = [[Integer alloc]initWithI:10]; 17 NSLog(@"retaincount = %lu",[i1 retainCount]);//1 18 19 20 //2.只通過(guò)指針賦值,不會(huì)獲得對(duì)象所有權(quán) 21 Integer *i2 = i1; 22 NSLog(@"retaincount = %lu",[i2 retainCount]);//1 23 24 25 //3.通過(guò)retain會(huì)獲得對(duì)象的所有權(quán) 26 [i1 retain]; 27 NSLog(@"retaincount = %lu",[i1 retainCount]);//2 28 29 30 //4.將對(duì)象添加到容器中,容器中會(huì)存儲(chǔ)對(duì)象的一個(gè)引用,會(huì)獲得對(duì)象所有權(quán) 31 NSMutableArray *array = [NSMutableArray array]; 32 [array addObject:i1]; 33 NSLog(@"retaincount = %lu",[i1 retainCount]);//3 34 35 36 //5.通過(guò)release釋放對(duì)象的所有權(quán) 37 [i1 release]; 38 NSLog(@"retaincount = %lu",[i1 retainCount]);//2 39 40 41 //6.從容器中刪除對(duì)象,也會(huì)釋放對(duì)象所有權(quán) 42 [array removeObject:i1]; 43 NSLog(@"retaincount = %lu",[i1 retainCount]);//1 44 45 //7.最后再釋放一次,對(duì)象才會(huì)被正常銷毀 46 [i1 release]; //此時(shí),底層會(huì)調(diào)用dealloc方法 //0 47 } 48 return 0; 49 }?
?測(cè)試結(jié)果是:
2015-08-13 17:32:36.408 02-MRC[1599:103515] retaincount = 1 2015-08-13 17:32:36.409 02-MRC[1599:103515] retaincount = 1 2015-08-13 17:32:36.410 02-MRC[1599:103515] retaincount = 2 2015-08-13 17:32:36.410 02-MRC[1599:103515] retaincount = 3 2015-08-13 17:32:36.410 02-MRC[1599:103515] retaincount = 2 2015-08-13 17:32:36.410 02-MRC[1599:103515] retaincount = 1 2015-08-13 17:32:36.410 02-MRC[1599:103515] integer dealloc Program ended with exit code: 0?
程序猿神奇的手,每時(shí)每刻,這雙手都在改變著世界的交互方式!本文轉(zhuǎn)自當(dāng)天真遇到現(xiàn)實(shí)博客園博客,原文鏈接:http://www.cnblogs.com/XYQ-208910/p/4727984.html,如需轉(zhuǎn)載請(qǐng)自行聯(lián)系原作者
總結(jié)
以上是生活随笔為你收集整理的Objective-C:MRC(引用计数器)获得对象所有权的方式(init、retain、copy等)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 理解 Lua 的那些坑爹特性
- 下一篇: C# 全选中数字文本框内容