2011-07-05 59 views
0

我有一個NSArray的這種方式IOS:問題同步的NSArray與串

myArray[0] = [string1, string2, string3, string4, mySecondArray, string5]; (at 0 position) 

我寫這個陣列的txt文件內以這種方式

NSString *outputString = @""; 

for (int i = 0; i< myArray.count; i++){ 

    outputString = [outputString stringByAppendingString:[[[myArray objectAtIndex:i ] componentsJoinedByString:@"#"] stringByAppendingString:@";"]]; 
} 

NSLog(@"string to write = %@", outputString); 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"Text.txt"]; 
NSError *error; 

[outputString writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error]; 

然後NSLog的結果爲= (位置myArray的0)(mySecond數組爲空)

one#two#three#four#(
)#five; 

我想知道:

  • 爲什麼數組包裝?
  • 當我去讀這個字符串時,我怎麼知道它是mySecondArray?

回答

0

當你一個NSArray對象的消息componentsJoinedByString:,它調用它的每個對象的description和連接它們才能。對於NSString對象,它們本身就是字符串。由於description方法已經實現,數組包裝。

至於識別陣列,而你正在讀回字符串,我不認爲這是可能的。你應該考慮數組寫入文件,而即

[[myArray objectAtIndex:0] writeToFile:filePath atomically:YES]; 

[myArray writeToFile:filePath atomically:YES]; 

取決於需求。這樣你就可以正確地讀取元素。