看板 MacDev 關於我們 聯絡資訊
看到書裡寫的一個範例程式,想請問一下為什麼在reversedString最後的 return reversed不需要加上retain或是autorelease?這樣local variable 不會在return後被release掉嗎? - (id)reversedString { unichar *buffer; int length, i, j, tmp; id reversed; if ((length = [content length]) <= 0) return @""; buffer = (unichar *)malloc(sizeof(unichar) * length); [content getCharacters:buffer range:NSMakeRange(0, length)]; for (i = 0, j= length-1; i < j; i++, j--) tmp = buffer[i], buffer[i] = buffer[j], buffer[j] = tmp; reversed = [NSString stringWithCharacters:buffer length:length]; free((void *)buffer); return reversed; ^^^^^^^^^^^^^^^^ } int main(int argc, const char * argv[]) { char buf[100] = {"test"}; id s, a, b, c, d, e; id pool = [[NSAutoreleasePool alloc] init]; //scanf("%s", buf); s = [NSString stringWithUTF8String:buf]; a = [[ReversibleString alloc] initWithString:s]; b = [[ReversibleString alloc] initWithString:@"Reverse?"]; printf("%s\n", [a UTF8String]); c = [[a reversedString] stringByAppendingString: b]; printf("%s\n", [c UTF8String]); [pool release]; return 0; } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 1.169.159.178
Blueshiva:[NSString stringWithCharacters:length:]傳回的已經 04/13 17:01
Blueshiva:是autorelease的東西了 04/13 17:02
naboo:原來如此,再請教一下,要怎麼知道傳回的已經是autorelease 04/13 17:15
naboo:的了?我查了一下Xcode裡的Documentation也沒看到 @@" 04/13 17:16
yllan:convention 04/13 17:53
Killercat:基本上絕大多數NS物件 用static喚起的都是含有下面 04/14 21:11
Killercat:幾個屬性 : alloc init autorelease 04/14 21:12
naboo:了解,謝謝大家熱心的幫我解答 04/15 18:26