2011-11-02 67 views
1

我有一個nsmutablearray有5個對象。iphonesdk追加字符串問題

例如,如果它包含像蘋果,貓,球,狗,螞蟻這樣的對象,我希望它們用逗號分隔,就像蘋果,貓,球,狗,螞蟻一樣。請幫我解決一下這個。

編輯

publishingpost //nsmutable array contains all these object 

-(ibaction)post 
{ 
nsstring *str; 
nsstring *str2 [email protected]","; 
for(int i = 0; i< [publishingpost count]; i++){ 
nsstring *str = [publishingpost objectAtIndex:i]; 
//append both str1, str2 to generate comma separated objects 

} 
+0

這個問題是有點混亂,法拉茲。是否有可能編輯它重新詞彙,這樣問題就會清楚了? –

回答

4

使用- (NSString *)componentsJoinedByString:(NSString *)separator代替:

- (IBAction)post { 
    NSString * str = [publishingpost componentsJoinedByString:@", "]; 
} 
0

看看stringByAppendingString:herestringByAppendingFormat。如果你已經有了字符串,應該很容易。

在for循環之前聲明一個NSString對象(或使用str)並在循環中追加該對象。而且你不應該在代碼中聲明str兩次。可能想要重命名其中的一個。