Re: [請益] NString要如何將字串串起來
※ 引述《uranusjr (←這人是超級笨蛋)》之銘言:
: ※ 引述《amos6064 (amos)》之銘言:
: 1)
: NSMutableString *result = [[NSMutableString alloc] init];
: NSString *input = [UILabel text];
: [result appendString:input];
: 2)
: NSString *result = [[NSString alloc] init];
: NSString *input = [UILabel text];
: [result autorelease];
順便說一下,這個 autorelease 加下去是沒有用的 :p
基本上,[[NSString alloc] init]、[NSString string],以及 @""
都是回傳同一個 static 的 NSString 物件,而這個物件的 retain
count 是 NSIntegerMax 。
: result = [[NSString alloc] initWithFormat:@"%@%@", result, input];
這個字串用完就記得要 release。
: 其實我也不知道哪個在什麼狀況比較好XD
: 在這個特定狀況中我會用 (2), 除非這個結合後的字串之後還要一直修改
如果是要跑個迴圈,把 100 個字串 append 到某個字串的話,
就會是用 NSMutableString 比較好。因為這樣就會變成是在迴圈
裡頭會建立一個 NSMutableString 物件,與一百個 NSString 的差別。
--
zonble.net
cocoa.zonble.net
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 114.44.187.245
→
02/02 02:07, , 1F
02/02 02:07, 1F
→
02/02 02:07, , 2F
02/02 02:07, 2F
→
02/02 02:14, , 3F
02/02 02:14, 3F
推
02/02 08:44, , 4F
02/02 08:44, 4F
討論串 (同標題文章)
完整討論串 (本文為第 3 之 3 篇):