2011-05-15 90 views
1

我遇到了一些問題。將NSMutableArray轉換爲字符串問題

我想一個數組保存到一個文本使用此代碼:

for(int aa = 0; aa <= [lines2 count]; aa++) { 
    content = [[NSString alloc] initWithFormat:@"%@;%@", content, [lines2 objectAtIndex:aa]]; 
} 

NSString *filePath = [[NSBundle mainBundle]pathForResource:@"cart" ofType:@"txt"]; //meine Zeile 
[content writeToFile:filePath atomically:NO encoding:NSStringEncodingConversionAllowLossy error:nil]; 

但它崩潰我的應用程序。任何人都可以給我一個簡單的例子,如何轉換NSMutable Array

One 
Two 
Three 

NSString

One;Two;Three 

希望有人可以幫我... :(

回答

1

你應該能夠使用稍簡單的代碼進行級聯,如下所示:

NSMutableString* content = [NSMutableString string]; 
for (int aa=0; aa < [lines2 count]; aa++){ 
    [content appendString:[NSString stringWithFormat:@"%@ ",[lines2 objectAtIndex:aa]]]; 
} 

或者,你可以使用componentsJoinedByString。

+0

非常感謝你.... nsmutablestring工作! – DennisW83 2011-05-15 18:21:06

+0

我會推薦componentsJoinedByString方法以獲得清晰度和Obj-C成語。 – 2011-05-16 06:58:50

0

也許更少,而不是less_or_equal?

for(int aa = 0; aa < [lines2 count]; aa++) 
+0

這可能會導致崩潰 – k06a 2011-05-15 18:15:05

+0

嗨,謝謝,我已經嘗試過,但它只是墜毀.. – DennisW83 2011-05-15 18:18:10