在我的应用程序中,我需要将值插入到只有最后一个路径组件的 NSDictionary 中。例如,我给定的 dict 是
a = 1;
b = 2;
c = 3;
d = {
e = 4;
f = 5;
g = {
h = 6;
};
};
j = {
k = 7;
};
我需要更改k
. 路径组件应该是@[@"j", @"k"]
. 我用类似的方法尝试过:
- (void)recurse:(NSDictionary*)dict keyToFound:(NSString*)ktf stack:(NSMutableArray*)stack parent:(NSString*)parent
{
for (NSString *key in [dict allKeys]) {
if ([key isEqualToString:ktf]) {
[stack insertObject:key atIndex:[stack count]];
return;
}
else {
if ([[dict valueForKey:key] isKindOfClass:[NSDictionary class]]) {
NSDictionary *d = [dict valueForKey:key];
[stack insertObject:key atIndex:[stack count]];
[self recurse:d keyToFound:ktf stack:stack parent:key];
//[stack removeObject:key];
}
}
}
}
但是,obviuos,这是一种错误的方式。