gpt4 book ai didi

ios - 添加 NSDictionary 作为 NSDictionary 键的值

转载 作者:行者123 更新时间:2023-11-28 19:58:29 27 4
gpt4 key购买 nike

我想添加一个不同的字典作为另一个键字典的值,但在我的循环中,我发现相同的值添加到我所有的字典键。这是我的代码:

for (MyObject *message in messages) {
int value = [key intValue] +1;

[mess setObject:message.body forKey:@"msg"];
[mess setObject:message.bareJidStr forKey:@"sender"];
[mess setObject:[element attributeStringValueForName:@"to"] forKey:@"reciever"];
[mess setObject:message.timestamp forKey:@"date"];
[[self messagesHistory] setObject:mess forKey:[NSString stringWithFormat:@"%d",value]];
}

有些东西不能正常工作,但我不知道是哪里!感谢您的帮助。

最佳答案

因为你在添加同一个字典

你必须在循环之前有这一行

NSMutableDictionary *mess = [NSMutableDictionary dictionary];

所以你只创建了一个字典并将它用作所有键的值

你需要在循环中移动这一行

for (MyObject *message in messages) {
// create a new dictionary for each value
NSMutableDictionary *mess = [NSMutableDictionary dictionary];
int value = [key intValue] +1;

[mess setObject:message.body forKey:@"msg"];
[mess setObject:message.bareJidStr forKey:@"sender"];
[mess setObject:[element attributeStringValueForName:@"to"] forKey:@"reciever"];
[mess setObject:message.timestamp forKey:@"date"];
[[self messagesHistory] setObject:mess forKey:[NSString stringWithFormat:@"%d",value]];
}

关于ios - 添加 NSDictionary 作为 NSDictionary 键的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25192935/

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