gpt4 book ai didi

iphone - 如何检查 NSDictionary 是否已经有一个键,然后在同一个键下添加项目?

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:30:00 25 4
gpt4 key购买 nike

我正在设置一个 NSDictionary 对象,以便 NSDictionary *scoreObjectkey 具有播放器的名称,然后是 { date 的可变字典: 对其 评分。为了获取数据,我拉取了一个我在 Parse 中创建的自定义类,它具有属性“Name”、“Score”和“createdAt”。

我正在尝试设置结构,以便上面的内容可以自动拉过 Parse 中的每一行数据,但是当我有两行相同的 Name 数据时,我遇到了麻烦,在我的 scoreObject 中设置为 keys。例如,如果 Bob 有两个分数和两个 createdAt 日期,我将如何简单地扩展 values 字典,以便它们仍然可以存储在键 = "Bob"下?

谢谢!

最佳答案

尝试这样的事情:

    NSDictionary *dict;
//this is the dictionary you start with. You may need to make it an NSMutableDictionary instead.


//check if the dictionary contains the key you are going to modify. In this example, @"Bob"
if ([[dict allKeys] containsObject:@"Bob"]) {
//there already is an entry for bob so we modify its entry
NSMutableDictionary *entryDict = [[NSMutableDictionary alloc] initWithDictionary:dict{@"Bob"}];
[entryDict setValue:@(score) forKey:@"Date"];
[dict setValue:entryDict forKey:@"Bob"];
}
else {
//There is no entry for bob so we make a new one
NSDictionary *entryDict = @{@"Date": @(score)};
[dict setValue:entryDict forKey:@"Bob"];
}

关于iphone - 如何检查 NSDictionary 是否已经有一个键,然后在同一个键下添加项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18007716/

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