0

在我的应用程序中,我需要将值插入到只有最后一个路径组件的 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,这是一种错误的方式。

4

3 回答 3

1

这是在嵌套字典中设置值的两种更简单的方法。第一个假设您知道密钥存在并且可能是 C 标识符。在这种情况下,使用setValue:forKeyPath:是最好的方法。否则,函数中的简单循环(或 NSDictionary 上的方法或类别)将起到作用:

void setValueForPathComponentsOfDictionary(id value, NSArray *components, NSMutableDictionary *dict) {

    NSMutableArray *parts = [components mutableCopy];
    id lastPart = parts.lastObject;
    [parts removeLastObject];

    for (id part in parts) {
        if (![dict respondsToSelector:@selector(objectForKey:)])
            return; // Silently fail.
        dict = [dict objectForKey:part];
    }

    if ([dict respondsToSelector:@selector(objectForKey:)])
        [dict setValue:value forKey:lastPart];
}

int main (int argc, const char * argv[])
{
    @autoreleasepool {

        NSMutableDictionary *dict = [@{
            @"a":[@{@"a":@1,@"b":@2} mutableCopy],
            @"b":[@{@"a":@3,@"b":@4} mutableCopy]
        } mutableCopy];

        NSLog(@"Initial Dictionary: %@", dict);

        [dict setValue:@9 forKeyPath:@"a.b"];
        NSLog(@"After setValue:forKeyPath: %@", dict);

        setValueForPathComponentsOfDictionary(@0, @[@"b",@"a"], dict);
        NSLog(@"After setValueForPathComponentsOfDictionary %@", dict);
    }
    return 0;
}
于 2013-05-03T16:46:55.250 回答
0

好的,我找到了解决方案。

- (BOOL)recurse:(NSDictionary*)dict keyToFound:(NSString*)ktf stack:(NSMutableArray*)stack parent:(NSString*)parent
{
    BOOL result = NO;
    for (NSString *key in [dict allKeys]) {
        if ([key isEqualToString:ktf]) {
            [stack insertObject:key atIndex:[stack count]];
            return YES;
        }
        else {
            if ([[dict valueForKey:key] isKindOfClass:[NSDictionary class]]) {
                NSDictionary *d = [dict valueForKey:key];
                [stack insertObject:key atIndex:[stack count]];
                result = [self recurse:d keyToFound:ktf stack:stack parent:key];
                if (!result) {
                    [stack removeLastObject];
                }
            }
        }
    }
    return result;
}
于 2013-05-03T15:58:10.623 回答
0
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:@1,@"a",
                      @1,@"b",
                      @2,@"c",
                      [NSMutableDictionary dictionaryWithObjectsAndKeys:@4, @"e",
                       @5, @"f",
                       [NSMutableDictionary dictionaryWithObject:@6 forKey:@"h"], @"g", nil],@"d",
                      [NSMutableDictionary dictionaryWithObject:@7 forKey:@"k"],@"i",nil];
NSLog(@"Before %@",dict);

NSString *keyToChange = @"e";
NSString *valueToChange = @"Your New Value";

[dict enumerateKeysAndObjectsUsingBlock:^(id key ,id obj, BOOL *stop){
    if([key isEqualToString:keyToChange])
    {
        NSLog(@"Found : %@",keyToChange);
        if(![obj isKindOfClass:[NSArray class]] || ![obj isKindOfClass:[NSDictionary class]] || ![obj isKindOfClass:[NSMutableDictionary class]])
        {
            [dict setValue:@"newval" forKey:keyToChange];
            return;
        }
    }
    if([obj isKindOfClass:[NSDictionary class]])
    {
        [obj enumerateKeysAndObjectsUsingBlock:^(id key ,id obj1, BOOL *stop){
            if([key isEqualToString:keyToChange])
            {
                if(![obj isKindOfClass:[NSArray class]] || ![obj isKindOfClass:[NSDictionary class]] || ![obj isKindOfClass:[NSMutableDictionary class]])
                {
                    NSLog(@"Found : %@",keyToChange);
                    [obj setValue:@"newval" forKey:keyToChange];
                    return;
                }
            }
        }];
    }
}];
NSLog(@"After %@",dict);
于 2013-05-03T16:14:04.050 回答