この 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];
このコードは機能します。
私の質問:これはこれを行うための最良の方法ですか? ディスクからのプロパティ リストの読み取りは問題ありません。しかし、プロパティ リストのすべての「レベル」を個別の変更可能なオブジェクトに保存し、その後、このすべてのオブジェクトをプロパティ リストの最高レベルまで逆に設定する必要がありますか?
より多くのレベルを持つプロパティ リストを想像しても、この方法でこれを行うのは少し複雑に思えます。この方法でそれを行うことが可能であれば、それは素晴らしいことです:
[temp setObject:[NSNumber numberWithInt:1] forKeyPath:@"profiles.0.size"];