更新多级plist目标c

时间:2014-07-06 19:21:40

标签: ios objective-c multidimensional-array plist

我能够毫无问题地从多级plist中读取数据。

我还可以使用以下内容编辑单维plist:

NSArray *pathsArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *doumentDirectoryPath=[pathsArray objectAtIndex:0];

NSString *destinationPath= [doumentDirectoryPath stringByAppendingPathComponent:@"data.plist"];

// Read from document directory
NSMutableDictionary *settingsItem = [[NSMutableDictionary alloc] initWithContentsOfFile:destinationPath];

// update the dictionary
[settingsItem setValue: [NSNumber numberWithInt:score] forKey:@"score"];

// write back to file
[settingsItem writeToFile:destinationPath atomically:YES];

但是我无法更新多级plist。

如何编辑看起来像这样的plist?

<plist version="1.0">
<dict>
    <key>level1</key>
    <dict>
        <key>level</key>
        <string>level1.tmx</string>
        <key>music</key>
        <string>level1.mp3</string>
        <key>score</key>
        <integer>0</integer>
    </dict>
    <key>level2</key>
    <dict>
        <key>level</key>
        <string>level2.tmx</string>
        <key>music</key>
        <string>level2.mp3</string>
        <key>score</key>
        <integer>0</integer>
    </dict>
</dict>

1 个答案:

答案 0 :(得分:3)

NSPropertyListSerializationNSPropertyListMutableContainersAndLeaves选项一起使用,以便返回的对象图的所有级别都是可变的。然后,您可以编辑对象并将其写回。