gpt4 book ai didi

ios - 编辑数组项内的键 - plist

转载 作者:行者123 更新时间:2023-11-28 21:59:27 25 4
gpt4 key购买 nike

我有以下 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>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>

我正在尝试编写和阅读

我已经制作了这两种方法,试图为特定的 subheadingName 读写 subheadingDetail,但它们是错误的/不完整的,无法让它工作。

-(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];
}

尝试搜索答案,但找不到与我的问题类似的内容。提前致谢。

最佳答案

您可以通过递归遍历所有字典和数组来做到这一点。下面是解决您的问题的示例代码。

    -(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 @"";
}

关于ios - 编辑数组项内的键 - plist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25460511/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com