【问题标题】:Classifying lists with inner lists使用内部列表对列表进行分类
【发布时间】:2017-12-17 11:08:31
【问题描述】:

我在以下结构中有 3 个模型

class Option {
    var id       : String  = ""
    var quantity : Int = 0

    init(_id: String, _quantity: Int) {
        id = _id
        quantity = _quantity
    }
}

class RemovedItems {
    var id       : String  = ""
    var quantity : Int = 0

    init(_id: String, _quantity: Int) {
        id = _id
        quantity = _quantity
    }
}

class ProductOrder {
    var guid    : String = ""
    var sizeHid : String = ""
    var options : [Option] = []
    var removed : [RemovedItems] = []

    init(id: String, sizeId: String, _options: [Option], removedItems: [RemovedItems]) {
        guid    = id
        sizeHid = sizeId
        options = _options
        removed = removedItems
    }
}

现在我有一个 ProductOrder 列表

[产品订单]

我想按选项和已删除列表过滤此列表。

productOrder1 具有带有 [A1, A2] 的选项列表,并使用 [C1] 删除

productOrder2 有带有 [A1, A2] 的选项列表,并使用 [] 删除

productOrder3 具有带有 [A1] 的选项列表,并使用 [C1] 删除

productOrder4 有带有 [A1, A2] 的选项列表,并使用 [C1] 删除

所以结果会显示 productOrder1, productOrder4 是相同的,因为它们具有相同的选项和删除的列表。

我可以通过遍历数组并使用一些逻辑来实现这一点,但我希望能够使用更高阶的函数和序列来做到这一点。简而言之,我想稍微清理一下我的代码。那么,如何做到这一点呢?

【问题讨论】:

    标签: ios swift generics filter sequence


    【解决方案1】:

    这是我基于EquatableforEach的解决方案,一次过滤:

    型号

    class Option: Equatable {
      var id       : String  = ""
      var quantity : Int = 0
    
      static func == (lhs: Option, rhs: Option) -> Bool {
        return lhs.id == rhs.id
      }
    
      init(_id: String, _quantity: Int) {
        id = _id
        quantity = _quantity
      }
    }
    
    class RemovedItems: Equatable {
      var id       : String  = ""
      var quantity : Int = 0
    
      static func == (lhs: RemovedItems, rhs: RemovedItems) -> Bool {
        return lhs.id == rhs.id
      }
    
      init(_id: String, _quantity: Int) {
        id = _id
        quantity = _quantity
      }
    }
    
    class ProductOrder: Equatable {
      var guid    : String = ""
      var sizeHid : String = ""
      var options : [Option] = []
      var removed : [RemovedItems] = []
    
      static func == (lhs: ProductOrder, rhs: ProductOrder) -> Bool {
        return lhs.options == rhs.options && lhs.removed == rhs.removed && lhs.guid != rhs.guid
      }
    
      init(id: String, sizeId: String, _options: [Option], removedItems: [RemovedItems]) {
        guid    = id
        sizeHid = sizeId
        options = _options
        removed = removedItems
      }
    }
    

    测试

    var orderProducts = [ProductOrder]()
    let o1 = Option(_id: "o1", _quantity: 1)
    let o2 = Option(_id: "02", _quantity: 1)
    let r1 = RemovedItems(_id: "r1", _quantity: 1)
    orderProducts.append(ProductOrder(id: "if3gpfubicurnwbviprgrv", sizeId: "_1234", _options: [o1, o2], removedItems: [r1]))
    orderProducts.append(ProductOrder(id: "if3gpfubicurnwbviprgrb", sizeId: "_1234", _options: [o1, o2], removedItems: [r1]))
    orderProducts.append(ProductOrder(id: "if3gpfubicurnwbviprgrc", sizeId: "_1234", _options: [o2], removedItems: []))
    orderProducts.append(ProductOrder(id: "if3gpfubicurnwbviprgrd", sizeId: "_1234", _options: [o2], removedItems: []))
    orderProducts.append(ProductOrder(id: "if3gpfubicurnwbviprgrj", sizeId: "_1235", _options: [o2], removedItems: [r1]))
    
    var results = [[ProductOrder]]()
    orderProducts.forEach {
      if let lastValue = results.last?.last, lastValue == $0 {
        results[results.count - 1].append($0)
      } else {
        results.append([$0])
      }
    }
    print(results)  //[[1,2], [3,4], [5]]
    

    【讨论】:

    • 这个解决方案看起来很有希望,但是结果不正确,在你提供的测试代码中,我想对p1、p3进行匹配。不是 p1 和 p2。因为 p1 和 p3 具有相同的选项,并且删除了列表。
    • 我明白了。已编辑,立即查看
    • 好的,结果你想要一个集合,对吧?
    • 已编辑。现在试试,我得到 [ [1,2], [3,4], [5] ]
    • 这太完美了,非常感谢。啤酒在我身上:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-13
    • 1970-01-01
    • 2021-02-05
    • 2020-03-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多