2010-06-08 64 views

回答

2

這要看你怎麼想使用該字符串。

將對象轉換爲字符串的一種方法是在該對象上調用-description(或-descriptionWithLocale:)。對於NSArray(或NSMutableArray) - 描述方法返回一個字符串,它表示接收方的內容,格式化爲屬性列表。你將得到的結果也將取決於如何在數組中的對象中實現-description方法。

5

這取決於你如何想要你的字符串。 一種方法可以遍歷數組並轉換它的每個元素。

NSMutableString * result = [[NSMutableString alloc] init]; 
for (NSObject * obj in array) 
{ 
    [result appendString:[obj description]]; 
} 
NSLog(@"The concatenated string is %@", result); 

您可以根據項目的類別修改上述代碼。

下面的代碼將數組轉換爲包含逗號和其他信息的字符串。

NSString * result = [array description]; 
14

如果你只想要數組的元素,那麼你可以嘗試componentsJoinedByString :.此方法返回所有包含分隔符字符串的元素,而不使用其他格式信息。

[array componentsJoinedByString:@","]; 

這裏「,」是分隔符字符串。