【问题标题】:Grouping 2 dictionaries in to an Array present in an Array in Swift在 Swift 中将 2 个字典分组到一个数组中
【发布时间】:2018-07-24 06:17:09
【问题描述】:

当我的数字数组像 -> ["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”

【问题讨论】:

  • 将 numbers 数组更改为 [{'id':1},{'id':2},{'id':3},{'id':4 时的预期结果是什么},{'id':5},{'id':6},{'id':7}]?
  • [ [{ }, { }], [{ }, { }], [{ }, { }], [{ }, { }] ]
  • ["1","2","3","4","5","6","7"] 是字符串数组,[{'id':1}] 是 [[string:Int]] 数组
  • @NeerajSonaro 像这样:[[["id": 1], ["id": 2]], [["id": 3], ["id": 4]], [["id": 5], ["id": 6]], [["id": 7]]]?
  • 那么我怎样才能达到这个结果..你能建议吗。

标签: ios arrays iphone swift


【解决方案1】:

您的代码看起来正确。问题在于您的输入数组。来自您的一条评论

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

您使用的是NSMutableArray,而不是快速的Array。 NSMutableArray 不能使用 CountableRange&lt;Int&gt; 下标

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

您需要使用 swift Array 来完成这项工作

【讨论】:

  • @NeerajSonaro,很高兴看到我的评论有所帮助。我也是这么想的。
【解决方案2】:

尝试以下解决方案:

let numbers = [["id":1],["id":2],["id":3],["id":4],["id":5],["id":6],["id":7]]//["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)])
}
print("chunks :: ", chunks)

// 打印为[[["id": 1], ["id": 2]], [["id": 3], ["id": 4]], [["id": 5], ["id": 6]], [["id": 7]]]

如果你只想要数字,试试下面的代码:

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

// 打印为[[1, 2], [3, 4], [5, 6], [7]]

【讨论】:

    【解决方案3】:

    如果这段代码

    [{'id':1},{'id':2},{'id':3},{'id':4},{'id':5},{'id':6},{'id':7}]

    Dictionary,那么你的策略应该是这样的:

    1. 将所有值放入一个普通数组中;
    2. 像你一样使用你的价值观。

    例子:

    let dict = [["id": 1], ["id": 2], ["id": 3], ["id": 4]]
    let numbers = dict.flatMap { $0.values } // [1, 2, 3, 4]
    let chunkSize = 2
    let chunks = stride(from: 0, to: numbers.count, by: chunkSize).map {
        Array(numbers[$0..<min($0 + chunkSize, numbers.count)])
    }
    print(chunks) // [[1, 2], [3, 4]]
    

    更新。

    我看到numbers 是同一个变量,但它可以包含不同的值。在这种情况下,您可以检查numbers的类型,然后使用适当的方式进行分组。

    var values: [Int] = []
    
    if let numbers = numbers as? [String] {
        values = numbers.map { Int($0) }
    } else if let numbers = numbers as? [[String, Int]] {
        values = numbers.compactMap { $0["id"] }
    }
    

    然后在values上使用跨步:

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

    注意,numbers 应该是 swift Array,而不是 NSMutableArray,因为 NSMutableArray 不能使用 CountableRange 来下标。

    【讨论】:

    • 实际上我有一个排除情况,其中一个包含 2 个字典组的数组满足给定条件......所以我需要它以同样的方式。
    • ( { "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;})
    • 看看我的实际数组是什么样子的。
    • @NeerajSonaro,你看到这个数组有什么问题。看起来这可以与您的原始代码一起正常工作。它不应该显示任何错误。
    • @adev -> var dataArray:NSMutableArray = NSMutableArray.init()
    猜你喜欢
    • 2015-10-04
    • 1970-01-01
    • 2020-05-30
    • 1970-01-01
    • 2021-12-07
    • 1970-01-01
    • 2019-10-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多