0

我有以下格式的 xml -

<plist version="1.0">
<array>
    <dict>...</dict>
    <dict>...</dict>
</array>
</plist>

我正在尝试使用 - 将其写入 plist

NSDictionary *temp = [NSPropertyListSerialization propertyListWithData:returnData options:NSPropertyListImmutable format:&format error:&errorDesc];
[self writeToPlistFile:@"myList.plist" : temp];

方法在哪里

-(BOOL)writeToPlistFile:(NSString*)filename:(NSDictionary*)data{
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString * documentsDirectory = [paths objectAtIndex:0];
NSString * path = [documentsDirectory stringByAppendingPathComponent:filename];
BOOL didWriteSuccessfull = [data writeToFile:path atomically:YES];
return didWriteSuccessfull;
}

然后我想阅读它并将其放入字典中。我该怎么做?

4

3 回答 3

2

如果 plist 以<array>标签开头,请使用此选项

NSMutableArray* documentArray = [NSMutableArray arrayWithContentsOfFile:sourcePath];

其中 sourcePath 是您的 plist 的路径。

于 2012-10-02T02:20:30.433 回答
0
-(void)saveData:(NSString*)data
{
    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentDirectory=[paths objectAtIndex:0];
        NSString *fileName=[NSString stringWithFormat:@"%@/%d.txt",documentDirectory,1];
        NSString *content=data;
        NSFileHandle *fileHandler= [NSFileHandle fileHandleForWritingAtPath:fileName];
        [fileHandler seekToEndOfFile];
        [fileHandler writeData:[content dataUsingEncoding:NSUTF8StringEncoding]];
        [fileHandler closeFile];
        NSLog(@"%@",fileName);
}
于 2012-10-02T01:51:57.747 回答
0

使用以下替换 filePath 与您用于保存它的相同路径。

NSDictionary *yourDictionary = [NSDictionary dictionaryWithContentsOfFile:filePath];
于 2012-10-01T21:34:45.557 回答