Objective-C:MRC(引用計數器)獲得對象所有權的方式(init、retain、copy等)

來源:互聯網
上載者:User

標籤:

     .h聲明檔案

 1 //  Integer.h 2 //  02-MRC 3 // 4 //  Created by ma c on 15/8/13. 5 //  Copyright (c) 2015年 bjsxt. All rights reserved. 6 // 7  8 #import <Foundation/Foundation.h> 9 10 @interface Integer : NSObject11 @property(nonatomic,assign)NSInteger i;12 -(id)initWithI:(NSInteger) i;13 -(void) print;14 +(Integer *)integerWithIntger:(NSInteger) i;15 @end

 

  .m實現檔案

 1 //  Integer.m 2 //  02-MRC 3 // 4 //  Created by ma c on 15/8/13. 5 //  Copyright (c) 2015年 bjsxt. All rights reserved. 6 // 7  8 #import "Integer.h" 9 10 @implementation Integer11 -(id)initWithI:(NSInteger) i12 {13     self = [super init];14     if(self)15     {16         _i = i;17     }18     return self;19 }20 +(Integer *)integerWithIntger:(NSInteger) i21 {22     return [[Integer alloc]initWithI:i];23 }24 25 -(void) print26 {27     NSLog(@"i = %ld",_i);28 }29 -(void)dealloc30 {31     NSLog(@"integer dealloc");32     [super dealloc];33 }34 @end

 

   主函數測試

 1 //  main.m 2 //  02-MRC 3 // 4 //  Created by ma c on 15/8/13. 5 //  Copyright (c) 2015年 bjsxt. All rights reserved. 6 // 7  8 #import <Foundation/Foundation.h> 9 #import "Integer.h"10 int main(int argc, const char * argv[])11 {12     @autoreleasepool13     {14         //測試手動引用計數15         //1.建立對象會獲得對象所有權16         Integer *i1 = [[Integer alloc]initWithI:10];17         NSLog(@"retaincount = %lu",[i1 retainCount]);//118         19         20         //2.只通過指標賦值,不會獲得對象所有權21         Integer *i2 = i1;22         NSLog(@"retaincount = %lu",[i2 retainCount]);//123         24         25         //3.通過retain會獲得對象的所有權26         [i1 retain];27         NSLog(@"retaincount = %lu",[i1 retainCount]);//228         29         30         //4.將對象添加到容器中,容器中會儲存物件的一個引用,會獲得對象所有權31         NSMutableArray *array = [NSMutableArray array];32         [array addObject:i1];33         NSLog(@"retaincount = %lu",[i1 retainCount]);//334         35         36         //5.通過release釋放對象的所有權37         [i1 release];38         NSLog(@"retaincount = %lu",[i1 retainCount]);//239         40         41         //6.從容器中刪除對象,也會釋放對象所有權42         [array removeObject:i1];43         NSLog(@"retaincount = %lu",[i1 retainCount]);//144         45         //7.最後再釋放一次,對象才會被正常銷毀46         [i1 release];  //此時,底層會調用dealloc方法     //047     }48     return 0;49 }

 

    測試結果是:

2015-08-13 17:32:36.408 02-MRC[1599:103515] retaincount = 12015-08-13 17:32:36.409 02-MRC[1599:103515] retaincount = 12015-08-13 17:32:36.410 02-MRC[1599:103515] retaincount = 22015-08-13 17:32:36.410 02-MRC[1599:103515] retaincount = 32015-08-13 17:32:36.410 02-MRC[1599:103515] retaincount = 22015-08-13 17:32:36.410 02-MRC[1599:103515] retaincount = 12015-08-13 17:32:36.410 02-MRC[1599:103515] integer deallocProgram ended with exit code: 0

 

Objective-C:MRC(引用計數器)獲得對象所有權的方式(init、retain、copy等)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.