我有以下的帖子:
<?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>Table 1</key>
<dict>
<key>subheadingArr</key>
<array>
<dict>
<key>subheadingName</key>
<string>Item 1 of table 1</string>
<key>subheadingDetail</key>
<string>Detail of item 1</string>
</dict>
<dict>
<key>subheadingName</key>
<string>Item 2 of table 1</string>
<key>subheadingDetail</key>
<string>Detail of item 2</string>
</dict>
</array>
</dict>
<key>Table 2</key>
<dict>
<key>subheadingArr</key>
<array>
<dict>
<key>subheadingDetail</key>
<string>Item 1 of table 2</string>
<key>subheadingName</key>
<string>Detail of item 1</string>
</dict>
<dict>
<key>subHeadingDetail</key>
<string>Item 2 of table 2</string>
<key>subheadingName</key>
<string>Detail of item 2</string>
</dict>
</array>
</dict>
</dict>
</plist>
我正在尝试编写和阅读
我已经让这两种方法尝试为特定的subheadingDetail
读写subheadingName
,但它们是错误/不完整的,并且无法使其正常工作。
-(void)updateInfo:(NSString *)info forSubHeadingName:(NSString *)subheadingName {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"DeviceInformation.plist"];
NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
[data setObject:info forKey:subheadingName];
[data writeToFile:path atomically:YES];
}
-(NSString *)readInfoForSubHeadingName:(NSString *)subheadingName {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"DeviceInformation.plist"];
NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
return [data objectForKey:subheadingDetail];
}
尝试搜索答案,但无法找到与我的问题类似的内容。 提前谢谢。
答案 0 :(得分:2)
您可以通过递归遍历所有字典和数组来实现。贝娄是解决问题的示例代码。
-(void)updateInfo:(NSString *)info forSubHeadingName:(NSString *)subheadingName
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"DeviceInformation.plist"];
NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
[self processAndSetObject:data parent:subheadingName Info:info];
[data writeToFile:path atomically:YES];
}
-(NSString *)readInfoForSubHeadingName:(NSString *)subheadingName
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"DeviceInformation.plist"];
NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
return [self processParsedObject:data parent:subheadingName];
}
-(void) processAndSetObject:(id)object parent:(NSString*)strKey Info:(NSString *)info
{
if([object isKindOfClass:[NSDictionary class]])
{
if([object objectForKey:@"subheadingName"] != nil)
{
if([[object valueForKey:@"subheadingName"] isEqualToString:strKey])
{
[object setValue:info forKeyPath:@"subheadingDetail"];
return;
}
}
for(NSString * key in [object allKeys])
{
id child = [object objectForKey:key];
[self processAndSetObject:child parent:strKey Info:info];
}
}
else if([object isKindOfClass:[NSArray class]])
{
for(id child in object)
{
[self processAndSetObject:child parent:strKey Info:info];
}
}
}
-(NSString *) processParsedObject:(id)object parent:(NSString*)strKey
{
if([object isKindOfClass:[NSDictionary class]])
{
if([object objectForKey:@"subheadingName"] != nil)
{
if([[object valueForKey:@"subheadingName"] isEqualToString:strKey])
{
return [object valueForKey:@"subheadingDetail"];
}
}
for(NSString * key in [object allKeys])
{
id child = [object objectForKey:key];
NSString *aStrRetVal = [self processParsedObject:child parent:strKey];
if(aStrRetVal.length > 0)
{
return aStrRetVal;
}
}
}
else if([object isKindOfClass:[NSArray class]])
{
for(id child in object)
{
NSString *aStrRetVal = [self processParsedObject:child parent:strKey];
if(aStrRetVal.length > 0)
{
return aStrRetVal;
}
}
}
return @"";
}
答案 1 :(得分:0)
您无法在Dictionary中编辑单个对象。你将再次写完整的plist。 为此你可以做一件事。获取所有plist字典。将其保存在字典对象中。然后编辑您不想编辑的细节。然后再次将完整的字典写入带有编辑细节的plist。这是实现这一目标的唯一方法。