gpt4 book ai didi

ios - 如何从两个 nsarrays 创建自定义 nsdictionary

转载 作者:搜寻专家 更新时间:2023-10-30 20:24:29 25 4
gpt4 key购买 nike

我有两个这样的数组

idArray:(
"548c2afe-5943-4929-9c6b-b544f5022115-sff45v-kjsa68f",
"656e8481-f2d5-4843-bebd-883f1b0f79d0-ksfns-gg456gg"
)

namesArray:(
"king",
"queen"
)

我想做一个像这样的字典

[{
"id" : "548c2afe-5943-4929-9c6b-b544f5022115-sff45v-kjsa68f",
"name" : "king"
},
{
"id" : "3656e8481-f2d5-4843-bebd-883f1b0f79d0-ksfns-gg456gg",
"name" : "queen"
}]

帮我解决这个问题。我试过了

NSDictionary *fullDictionary = @{@"id": userarray, @"scandat": scandatesarray};

NSError *error;
NSData *data = [NSJSONSerialization dataWithJSONObject:fullDictionary options:0 error:&error];
NSLog(@"%@", fullDictionary);

输出是

{
id = (
"656e8481-f2d5-4843-bebd-883f1b0f79d0",
"548c2afe-5943-4929-9c6b-b544f5022115"
);
scandat = (
"2016-05-28 16:50:30",
"2016-05-28 16:50:59"
);
}

最佳答案

您不是在制作字典 - 您的目标对象是一个包含 NSDictionary 对象作为其元素的 NSArray

NSMutableArray *res = [NSMutableArray array];
NSArray *keys = @[@"id", @"name"];
for (int i = 0 ; i != idArray.count ; i++) {
NSDictionary *dict = [NSDictionary
dictionaryWithObjects: @[idArray[i], nameArray[i]]
forKeys: keys
];
[res addObject:dict];
}

关于ios - 如何从两个 nsarrays 创建自定义 nsdictionary,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37498248/

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