2011年8月13日 星期六

Why NSString* retainCount is 2147483647

先來看以下這段程式碼:

NSString *test1 = @"Test string";
NSLog(@"test1 retain count is: %d", [test1 retainCount]);	// 2147483647
[test1 retain];
NSLog(@"test1 retain count is: %d", [test1 retainCount]);	// 2147483647
[test1 release];
NSLog(@"test1 retain count is: %d", [test1 retainCount]);	// 2147483647

恩,你沒看錯,印出來的 retainCount 都是 2147483647,
接著我們再來看下面這段程式碼:

NSString *test2 = [[NSString alloc] initWithString:@"Test string"];
NSLog(@"test 2 retain count is: %d", [test2 retainCount]);	// 猜猜看
[test2 retain];
NSLog(@"test 2 retain count is: %d", [test2 retainCount]);	// 猜猜看
[test2 release];
NSLog(@"test 2 retain count is: %d", [test2 retainCount]);	// 猜猜看


恩,印出來的值有符合預期的物件參考值:1 2 1 嗎?
答案是沒有,同樣都是 2147483647 這個神奇數字,這個數字到底是什麼東西呢?

其實上述的寫法,在經過 xcode compiler 編譯之後,都會把它改成一個指向常數字串的指標,
而這個神奇的數字,就是因為指標指向常數的時候,使用 retainCount 只會印出 UINT_MAX,
實際上看以下的圖就會更瞭解:



接著再來看這段程式碼:

NSString *test3 = [[NSString alloc]initWithFormat:@"%@", @"Test string"];
NSLog(@"test 3 retain count is: %d", [test3 retainCount]);	// 1	
[test3 retain];
NSLog(@"test 3 retain count is: %d", [test3 retainCount]);	// 2
[test3 release];
NSLog(@"test 3 retain count is: %d", [test3 retainCount]);	// 1	

這樣就有符合預期的參考值了,差異就在 initWithFormat 這個初始字串的方式,
看出差異了嗎?

沒有留言: