ios 如何使用键将对象添加到 NSMutableDictionary?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16644055/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:38:07  来源:igfitidea点击:

How do i add object to NSMutableDictionary with key?

iosobjective-cnsdictionarynsmutabledictionary

提问by Ajay Chaudhary

I have plist ,i want add the elememts from that plist to a mutable dictionary .First i check the user name in plist and my current name are same or not if its same then i wanna add all value to that dictionary rom plist and also assign the same key?

我有 plist ,我想将该 plist 中的元素添加到可变字典中。首先我检查 plist 中的用户名,我当前的名称是否相同,如果相同,然后我想将所有值添加到该字典 rom plist 并分配同样的钥匙?

 for(int i=0;i<[displayList count];i++)
    {
        if([[[data valueForKey:@"username"] objectAtIndex:i] isEqualToString:user])
        {
            [finalBooks setValue:[[data valueForKey:@"ID"] objectAtIndex:i] forKey:@"ID"];
            [finalBooks setValue:[[data valueForKey:@"name"] objectAtIndex:i] forKey:@"name"];
            [finalBooks setValue:[[data valueForKey:@"Place"] objectAtIndex:i] forKey:@"Place"];
          [finalBooks setValue:[[data valueForKey:@"username"] objectAtIndex:i] forKey:@"username"];


        }
    }

[displayList count] this value is 7 so each value replacing 6 times ,and am getting only final valuesi want every values matching to user name i that mutabledictionary

[displayList count] 这个值是 7,所以每个值替换 6 次,并且我只得到最终值我希望每个值都匹配到用户名我那个可变字典

回答by Ajay Chaudhary

I think you should create array of dictionary. Just add your finalBooks dictionary to NSMutableArray like this

我认为你应该创建字典数组。只需像这样将您的 finalBooks 字典添加到 NSMutableArray

NSMutableArray *finalArray = [[NSMutableArray alloc]init];

(int i=0;i<[displayList count];i++)
{
    if([[[data valueForKey:@"username"] objectAtIndex:i] isEqualToString:user])
    {
        [finalBooks setValue:[[data valueForKey:@"ID"] objectAtIndex:i] forKey:@"ID"];
        [finalBooks setValue:[[data valueForKey:@"name"] objectAtIndex:i] forKey:@"name"];
        [finalBooks setValue:[[data valueForKey:@"Place"] objectAtIndex:i] forKey:@"Place"];
        [finalBooks setValue:[[data valueForKey:@"username"] objectAtIndex:i] forKey:@"username"];

        [finalArray addObject:finalBooks];
    }
}

And you will get array of dictionary. Good Luck !!

你会得到字典数组。祝你好运 !!

回答by Gihan

setValue method in a NSMutableDictionary replaces the value if the key is same. So what you can do is either use NSMutableDictionary of arrays or dictionaries.

如果键相同,NSMutableDictionary 中的 setValue 方法将替换该值。所以你可以做的是使用数组或字典的 NSMutableDictionary。

NSMutableDictionary *userDictionary=[[NSMutableDictionary alloc]init];

(int i=0;i<[displayList count];i++)
{
    if([[[data valueForKey:@"username"] objectAtIndex:i] isEqualToString:user])
    {
        [finalBooks setValue:[[data valueForKey:@"ID"] objectAtIndex:i] forKey:@"ID"];
        [finalBooks setValue:[[data valueForKey:@"name"] objectAtIndex:i] forKey:@"name"];
        [finalBooks setValue:[[data valueForKey:@"Place"] objectAtIndex:i] forKey:@"Place"];
        [finalBooks setValue:[[data valueForKey:@"username"] objectAtIndex:i] forKey:@"username"];

        [userDictionary setObject:finalBooks forKey:user];
    }
}

If you want everything in one dictionary, I suggest you use a unique key such as NSString stringWithFormat:@"ID+%@",username],...[NSString stringWithFormat:@"username+%@",username]

如果您希望在一本字典中包含所有内容,我建议您使用唯一键,例如 NSString stringWithFormat:@"ID+%@",username],...[NSString stringWithFormat:@"username+%@",username]

But it looks messy :)

但它看起来很乱:)