如何编辑多个级别的属性列表?

时间:2014-07-17 15:49:53

标签: ios cocoa nsdictionary nsmutabledictionary property-list

我想更改此属性列表profileData.plist中的值:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>profiles</key>
    <array>
        <dict>
            <key>Name</key>
            <string>Default Profile</string>
            <key>size</key>
            <integer>0</integer>
        </dict>
    </array>
    <key>settings</key>
    <dict>
        <key>length</key>
        <string>cm</string>
    </dict>
</dict>
</plist>

我想将键size的整数设置为1。我这样做了:

// reading Property List as described in Property List Programming Guide
NSError *error = nil;
NSPropertyListFormat format;
NSString *plistPath;
NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                     NSUserDomainMask, YES) objectAtIndex:0];
plistPath = [rootPath stringByAppendingPathComponent:@"profileData.plist"];
if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {
    plistPath = [[NSBundle mainBundle] pathForResource:@"profileData" ofType:@"plist"];
}
NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];
NSMutableDictionary *temp = (NSMutableDictionary *)[NSPropertyListSerialization
                                      propertyListWithData:plistXML
                                      options:NSPropertyListMutableContainersAndLeaves
                                      format:&format
                                      error:&error];
if (!temp) {
    DNSLog(@"Error reading plist: %@, format: %ld", error, (long)format);
}

// getting the right dictionary
NSMutableArray *profiles = [temp objectForKey:@"profiles"];
NSMutableDictionary profile0 = [profiles objectAtIndex:0];

// Setting new integer
[profile0 setObject:[NSNumber numberWithInt:1] forKey:@"size"];

// saving objects in reverse
[profiles replaceObjectAtIndex:0 withObject:profile0];
[temp setObject:profiles forKey:@"profiles"];

// writing property list
[temp writeToFile:plistPath atomically:YES];

此代码有效。

我的问题:这是最好的方法吗?从磁盘读取属性列表是可以的。但保存所有&#34;等级是否有必要?在单独的可变对象中的属性列表,然后将所有这些对象反向设置为属性列表的最高级别?

对我而言,即使我想象一个具有更多级别的属性列表,以这种方式执行此操作似乎有点复杂。如果以这种方式做到这一点会很好:

[temp setObject:[NSNumber numberWithInt:1] forKeyPath:@"profiles.0.size"];

1 个答案:

答案 0 :(得分:0)

/*The nature of using a plist or XML in XCode requires you to load the entire contents of the file into memory before doing anything with it (unless you're using a 3rd-party DOM parser for XML), just as you've done here. NSArray & NSDictionary then have the `writeToFile: atomically:` method you're already using to write changes back to disk. 

Of course you can access and change the temp NSMutableDictionary at whichever level you like, but if you want to save the changes then this is pretty much how you have to do it. 

As you imagined, this can be bad for larger/complex data or if you're saving changes all the time. 

I'd suggest you refactor this into multiple methods - right now you're loading the data, making the changes, AND writing back out to file all in the same place, which means you have to do all of that each time.

Break things up so you can do these tasks independently:

 - load plist from disk 
 - create temp copy of plist data as @property
 - make a change to tempdata @property (passing in whatever argument you need); 
 - write changes to disk

This would allow you to make multiple changes to the temp data as needed & only update the data model on disk when you have a bunch to do at once or need to ensure it's up to date (say, before the app goes into the background). This would cut the # of read/writes, but requires you to consider how fresh you need your data model to be & when's the best time to update the changes. 

If you're not stuck w/using plists, you could also look into using JSON, which is faster & lighter, and XCode now has pretty good native support, but writing to a file is the exact same process. For persisting larger, more complex data sets and making easier changes to individual items you might want to consider CoreData.   

..................................
1 other note ... In these lines: 

    NSMutableArray *profiles = [temp objectForKey:@"profiles"];
    NSMutableDictionary profile0 = [self.profiles objectAtIndex:0];

You're declaring `*profiles` within the method, not as a property of the object, so you shouldn't refer to it as `self.profiles`. Surprised this works w/o causing an error (unless you have both? In that case it won't work as expected). After refactoring to separate methods an @property is what you'll want.*/

基于以下评论编辑:

我知道 - 在这种情况下,您上面的建议似乎非常接近,但方法名称为setValue:(id) forKeyPath:(NSString *),而不是setObject...。此外,您可以使用NSNumber的Obj-C文字语法。

也许尝试类似:     [temp setValue:@1 forKeyPath:@"temp.profiles.0.size"];

(注意:尚未对此进行测试。不确定您是否需要在keyPath中包含temp的完整路径)。 请参阅Apple的KVC programming guideKVC Protocol reference

我不认为这种方法会对速度或性能产生明显影响,但我还没有对其进行测试。这也假定您访问的对象肯定是具有那些特定键的字典。如果结构或密钥发生变化,那么这里没有内省或错误处理。您最初的方法的一个好处是您明确了解对象类型&amp;他们可以访问的方法/属性,而这是开放式的。