Re: [請益] NString要如何將字串串起來

看板MacDev作者 (zonble)時間13年前 (2011/02/02 01:53), 編輯推噓1(103)
留言4則, 2人參與, 最新討論串3/3 (看更多)
※ 引述《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
我是為了舉例...不然我改一下好了, 本來是要像原 po 寫的
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
文章代碼(AID): #1DI4YKjf (MacDev)
文章代碼(AID): #1DI4YKjf (MacDev)