2011-07-05 76 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]; 

取决于需求。这样你就可以正确地读取元素。