你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS 切換到MRC環境下打印 retainCount 一直是-1或許一長串證書

iOS 切換到MRC環境下打印 retainCount 一直是-1或許一長串證書

編輯:IOS開發綜合

依據蘋果的官方文檔https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Protocols/NSObject_Protocol/Reference/NSObject.html

下面寫著應該前往的是這個對象的UINT_MAX, 並且不會釋放, 這個UINT_MAX

最大二進制為(11111111111111111111111111111111)也是十六進制(0xffffffff)
假如你當成有符號數取補碼後輸入就得-1;
假如你當成無符號數就是最大數即:4294967295


2.在X64的機器上輸入是9223372036854775807,int的最大值 在I386的機器上輸入是2.由於NSNumber的numberWithInteger發生的對象是屬於autorelease pool
在協助文檔中
可以看到這麼一句話:
The retainCount method does not account for any pending autorelease messages send to the receiver.
就是說retainCount 關於autorelease音訊發生的的對象,並不牢靠。


3.Donot use this method. (required) You should never use-retainCount, because it never tells you anything useful. 永遠不要用這個辦法,只需恪守alloc,neworcopy以及任何需求分配內存的時分調用release就可以了

參考文檔:


http://www.friday.com/bbum/2011/12/18/retaincount-is-useless/。


4.IOS中的 NSString NSArray 這種IOS本身的對象貌似都做了一些優化,打印其retainCount的數值都是-1這種狀況。

但是我們自己創立一個類

eg:Test.h

用它來創立的對象是完全和我們之前了解的retainCount預期值是相反的。


NSString *string = [[NSString alloc] init];

[string retain];

NSLog(@"string = %ld", (unsigned long)[string retainCount]);

NSLog(@"string = %lu", (unsigned long)[string retainCount]);


NSString *string2 = [[NSString alloc] initWithString:@"123"];

NSLog(@"string2 = %ld", (unsigned long)[string2 retainCount]);

NSLog(@"string2 = %lu", (unsigned long)[string2 retainCount]);

NSString *string3 = [NSString string];

NSLog(@"string3 = %ld", (unsigned long)[string3 retainCount]);

NSLog(@"string3 = %lu", (unsigned long)[string3 retainCount]);

NSString *string4 = [NSString stringWithFormat:@"123%@",@"123"];

NSLog(@"string4 = %ld", (unsigned long)[string4 retainCount]);

NSLog(@"string4 = %lu", (unsigned long)[string4 retainCount]);

Test *test = [[Test alloc] init];

[test retain];

[test release];

NSLog(@"test = %lu", (unsigned long)[test retainCount]);


以上代碼的打印:

2016-12-20 17:29:17.103 acm[40218:510858] string = -1

2016-12-20 17:29:17.103 acm[40218:510858] string = 18446744073709551615

2016-12-20 17:29:17.103 acm[40218:510858] string2 = -1

2016-12-20 17:29:17.103 acm[40218:510858] string2 = 18446744073709551615

2016-12-20 17:29:17.104 acm[40218:510858] string3 = -1

2016-12-20 17:29:17.104 acm[40218:510858] string3 = 18446744073709551615

2016-12-20 17:29:17.104 acm[40218:510858] string4 = -1

2016-12-20 17:29:17.104 acm[40218:510858] string4 = 18446744073709551615

2016-12-20 17:29:17.104 acm[40218:510858] test = 1



【iOS 切換到MRC環境下打印 retainCount 一直是-1或許一長串證書】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved