gpt4 book ai didi

ios - 使用内部列表对列表进行分类

转载 作者:行者123 更新时间:2023-11-28 06:07:12 25 4
gpt4 key购买 nike

我有以下结构中的 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 列表

[ProductOrder]

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

productOrder1 has options list with [A1, A2], and removed with [C1]

productOrder2 has options list with [A1, A2], and removed with []

productOrder3 has options list with [A1], and removed with [C1]

productOrder4 has options list with [A1, A2], and removed with [C1]

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

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

最佳答案

这是我基于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]]

关于ios - 使用内部列表对列表进行分类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47854465/

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