ios - 合并数组形成字典 - iOS Objective C

标签 ios objective-c

我有两个像这样的二维数组

Array1 = [(1, 2), (3, 4), (5, 6)]

Array2 = [(x, y), (x, y), (x, y)]

我想形成一个像 "coordinates"= [{"x":"1", "y":"2"}, {"x":"3", "y":"这样的字典4"},{"x":"5", "y":"6"}]

我该怎么做?

最佳答案

正如您在评论中提到的,第二个数组总是有 x 和 y,那么无需循环该数组,您只需要像这样迭代第一个数组。

NSMutableArray *dicArray = [[NSMutableArray alloc] init];
for (NSArray *subArray in array1) {
    NSDictionary *dic = @{ @"x": [subArray firstObject], @"y": [subArray lastObject] };
    [dicArray addObject:dic];
}
NSLog(@"%@",dicArray);

关于ios - 合并数组形成字典 - iOS Objective C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39304261/

相关文章:

ios - 使用 Firebase 的 iOS 应用程序的电子邮件验证

ios - 使用隐藏属性与私有(private) iVars

objective-c - 按下按钮时应用程序崩溃

objective-c - 什么是回调处理程序

iphone钥匙串(keychain)项目在应用程序卸载后仍然存在?

objective-c - UI 状态栏淡入淡出时间 iOS

c# - NSArray 的、原始类型和拳击哦,我的天哪!

ios - View 不在状态栏下方且状态栏为黑色

ios - 有没有办法注册一个属性依赖于另一个属性?

ios - XCode 获取 "target specifies product type ' com.apple.product-type.bundle.unit-test',但 'iphoneos' 平台没有此类产品类型”