2010-08-13 104 views
0

我想用逗號作爲逗號分隔字符串作爲分隔符,並且結果必須存儲在字符串中...... comma = @「,」; (i = 0; i < [resources count]; i ++) { Record * aRecord = [resources objectAtIndex:i];如何將值連接到字符串

temp=aRecord.programID; 
    if(i==0) 
     pid=temp; 
    else 
    //i am using this one to cancatenate but is not working why? 

pid = [NSString stringWithFormat:@「%@%@%@」,pid,comma,temp]; }

+0

你沒有說清楚。你有什麼形式的字符串串聯?你想對結果做什麼?顯示一些代碼。 – 2010-08-14 15:45:47

回答

5

使用上NSArray-componentsJoinedByString:方法:

NSArray *csvArray = [NSArray arrayWithObjects:@"here", @"be", @"dragons", nil]; 
NSLog(@"%@", [csvArray componentsJoinedByString:@", "]); 

(從the docs

+0

ishould have to store result in string ... – 2010-08-14 15:42:11

+0

@lak:是的,這樣做。這是NSArray上的一個返回NSString的方法。查看文檔,或嘗試一下。 – 2010-08-14 16:29:03

1

id類型轉換爲NSString,然後使用NSString的類引用中找到的級聯方法。