2010-10-27 284 views
2

我試圖使用顯示在數組中的數據項數組項下:字符串輸出

NSString *alertString = [NSString stringWithFormat:@"%@", path]; 

工作正常,但是當我展示它就會顯示在下面的方式字符串:


A,
B,
C,
d

有沒有辦法讓它以不同的方式顯示,如全部在一行並沒有括號,逗號或行返回是這樣的:

ABCD

回答

13

您有幾個選項。它看起來像陣列中的對象有一個description方法將它們打印你想要的方式,因此它可能是簡單的使用:

NSString *alertString = [path componentsJoinedByString:@" "]; 

如果沒有,你可以考慮這樣的事情:

NSMutableString *s = [NSMutableString string]; 
[path enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { 
    [s appendString:@" "]; 
    [s appendString:[obj someMethodThatFormatsTheObject]]; 
}]; 
NSString *alertString = [NSString stringWithString:s]; 

甚至:

NSMutableArray *a = [NSMutableArray array]; 
[path enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { 
    [a addObject:[obj sometMethodThatFormatsTheObject]]; 
}]; 
NSString *alertString = [a componentsJoinedByString:@" "]; 
+0

選擇這一個純粹是因爲它詳細說明。謝謝您的幫助 :) – 2010-10-28 11:14:02