将2个字典分组到Swift中数组中存在的数组中 [英] Grouping 2 dictionaries in to an Array present in an Array in Swift

查看:49
本文介绍了将2个字典分组到Swift中数组中存在的数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我的数字数组为-> [ 1, 2, 3, 4, 5, 6, 7]

When my numbers array is like -> ["1","2","3","4","5","6","7"]

let numbers = ["1","2","3","4","5","6","7"]
let chunkSize = 2
let chunks = stride(from: 0, to: numbers.count, by: chunkSize).map {
    Array(numbers[$0..<min($0 + chunkSize, numbers.count)])
}

// prints as [["1", "2"], ["3", "4"], ["5", "6"], ["7"]]

但是当我的数字数组为

   (
        {
        "facility_id" = 1;
        "options_id" = 3;
    },
        {
        "facility_id" = 3;
        "options_id" = 12;
    },
        {
        "facility_id" = 2;
        "options_id" = 7;
    },
        {
        "facility_id" = 3;
        "options_id" = 12;
    },
        {
        "facility_id" = 2;
        "options_id" = 6;
    },
        {
        "facility_id" = 1;
        "options_id" = 4;
    }
  ) 

它显示此错误:


无法将类型'CountableRange'的值转换为预期参数类型'Int'

Cannot convert value of type 'CountableRange' to expected argument type 'Int'


推荐答案

您的代码看起来正确。问题出在您的输入数组上。根据您的评论

Your code looks correct. Problem is with your input array. From one of your comment


@adev-> var dataArray:NSMutableArray = NSMutableArray.init()–

@adev -> var dataArray:NSMutableArray = NSMutableArray.init() –

您使用的是 NSMutableArray ,而不是快速的 Array 。 NSMutableArray不能使用 CountableRange< Int>

you are using NSMutableArray, not swift Array. NSMutableArray cannot be subscripted using a CountableRange<Int>

$0..<min($0 + chunkSize, numbers.count)  //returns CountableRange<Int>

您需要使用Swift Array 进行这项工作

You need to use swift Array to make this work

这篇关于将2个字典分组到Swift中数组中存在的数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆