首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从键数组添加到字典

问从键数组添加到字典
EN

Stack Overflow用户
提问于 2012-06-04 01:53:01
回答 3查看 366关注 0票数 0

在过去的几天里,我一直在努力解决以下问题

我需要在字典中设置一个键和一个很深的值。我知道字典的关键字,我想在其中设置关键字,但我不知道如何到达那里。密钥存储在数组中。

比方说,我有一个包含以下关键字的字典,每个关键字的值是另一个字典。

代码语言:javascript
复制
- myDict
    - A
      - A
        - A
        - B
      - B
        - A
        - B
    - B
      - A
        - A
        - B
      - B
        - A
        - B

我还有以下数组:

代码语言:javascript
复制
[A, A, B]

这意味着,我希望为字典中的A->A->B中的键设置一个值。这等于以下内容:

代码语言:javascript
复制
[[[[myDict objectForKey:@"A"] objectForKey:@"A"] objectForKey@"B"] setValue:myValue forKey:myKey]

有谁知道我有什么方法可以做到这一点吗?我设想我应该保持对字典的引用,迭代数组中的对象并转到字典中的“下一级”,并保留对此的引用,当到达数组中的最后一个对象时,设置键的值。我的问题是,我不知道该怎么做。为了保持对字典的引用,当我进入下一级时,我需要初始化一个新的字典,对吗?这会导致我创建一个只包含当前级别的对象的新字典,因此我将不会在原始字典中设置键的值。

任何代码示例或伪代码都将非常受欢迎。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-06-04 03:22:35

在我看来,您只需执行[yourArray componentsJoinedByString:@"."]来获取密钥路径,然后使用valueForKeyPath:来获取适当的值。

票数 1
EN

Stack Overflow用户

发布于 2012-06-04 02:20:50

你会做这样的事情,不是说这是“确切”的答案,而是应该让你找到你的确切需求。

代码语言:javascript
复制
for(NSString* key in [myDict allKeys]) {

    NSString *yourValue = ....
    NSDictionary *dict = [[myDict objectForKey:key] objectForKey:key];// Will get you to A->A

    for(NSString *levelKey in [dict allKeys]) {

    //Setting Value for A->A->(A) & A->A->(B)

          [dict setValue:yourValue forKey:levelKey];      
    }

}
票数 0
EN

Stack Overflow用户

发布于 2012-06-04 03:14:27

此答案应忽略。相反,使用@Chuck发布的方法。

我最终写了下面两个方法来解决这个问题。这是由@nhahtdh启发的。这些方法很适合一个类别,但我以前从来没有写过一个类别。总有一天,我会把它们归为一个类别。如果有人想这样做,请在这里张贴结果。

代码语言:javascript
复制
/**
 *  Sets the value in a dictionary with a keypath. Uses either the first or the last key in the keypath as the key to set the value for.
 *  @param value Value to set.
 *  @param dictionary Dictionary to set the value in.
 *  @param path Path for the key. Last or first object is used as the key for the value.
 *  @param readFromEnd Whether or not to read the path from the end.
 */
 + (void)setValue:(id)value inDictionary:(NSMutableDictionary *)dictionary withKeyPath:(NSArray *)path readFromEnd:(BOOL)readFromEnd
{
    NSMutableArray *mutablePath = [NSMutableArray arrayWithArray:path];
    NSString *key;
    if (readFromEnd)
    {
        key = [mutablePath lastObject];
        [mutablePath removeLastObject];
    }
    else
    {
        key = [mutablePath objectAtIndex:0];
        [mutablePath removeObjectAtIndex:0];
    }
    [self setValue:value forKey:key inDictionary:dictionary withKeyPath:mutablePath readFromEnd:readFromEnd];
}

/**
 *  Sets the value in a dictionary with a keypath.
 *  @param value Value to set.
 *  @param key Key for the value.
 *  @param dictionary Dictionary to set the value in.
 *  @param path Path for the key.
 *  @param readFromEnd Whether or not to read the path from the end.
 */
+ (void)setValue:(id)value forKey:(NSString *)key inDictionary:(NSMutableDictionary *)dictionary withKeyPath:(NSArray *)path readFromEnd:(BOOL)readFromEnd
{
    if (path.count == 0)
        [dictionary setValue:value forKey:key];
    else
    {
        NSMutableArray *mutablePath = [NSMutableArray arrayWithArray:path];
        NSString *currentKey;
        if (readFromEnd)
        {
            currentKey = [mutablePath lastObject];
            [mutablePath removeLastObject];
        }
        else
        {
            currentKey = [mutablePath objectAtIndex:0];
            [mutablePath removeObjectAtIndex:0];
        }
        [self setValue:value forKey:key inDictionary:[dictionary objectForKey:currentKey] withKeyPath:mutablePath readFromEnd:readFromEnd];
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10872611

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档