Ios 计算集合中项目的出现次数
我正在寻找一种方法来计算相同的产品添加到订单中的次数(我是Swift新手) 我想得到以下结果,但我找不到方法:Ios 计算集合中项目的出现次数,ios,swift,Ios,Swift,我正在寻找一种方法来计算相同的产品添加到订单中的次数(我是Swift新手) 我想得到以下结果,但我找不到方法: currentOrder.list() --> product_1 x2 // product_2 x1 // product_4 x1 我只能列出包含以下内容的所有产品: //Inside struc Order func listProductsSold() { if productsSold.count>0 { for product in produc
currentOrder.list() --> product_1 x2 // product_2 x1 // product_4 x1
我只能列出包含以下内容的所有产品:
//Inside struc Order
func listProductsSold() {
if productsSold.count>0 {
for product in productsSold {
product.printProduct()
}
}
}
//Inside struc Product
func printProduct() {
print("Nom: \(name), price: \(price), couleur: \(color)")
}
然后:
currentOrder.listProductsSold()
//Which give:
//Nom: Menu cheese, price: 17.0, couleur: rgb(247, 171, 56)
//Nom: Menu cheese, price: 17.0, couleur: rgb(247, 171, 56)
//Nom: Rouge n1, price: 18.0, couleur: rgb(166, 77, 121)
//Nom: Vin vigneron, price: 22.0, couleur: rgb(166, 77, 121)
首先需要一个“groupby”函数将所有相同的产品收集到一个数组中。此类功能的示例可在中找到
公共扩展序列{
func组(按键:(Iterator.Element)->U)->[U:[Iterator.Element]]{
变量类别:[U:[迭代器.元素]]=[:]
自我元素{
让键=键(元素)
如果case nil=类别[键]?.append(元素){
类别[键]=[元素]
}
}
退货类别
}
}
然后你可以这样做:
let orderSummary = currentOrder.productsSold
.group { $0.name }
.sorted { $0.key < $1.key }
.map { "\($0.key) x \($0.value.count)" }
.joined(separator: ", ")
让orderSummary=currentOrder.productsSold
.group{$0.name}
.sorted{$0.key<$1.key}
.map{“\($0.key)x\($0.value.count)”}
.已联接(分隔符:“,”)
它的作用是:
将.group
分组到一个字典中,字典的键是产品名称,值是具有相同名称的产品数组产品sold
按键(即产品名称)对键值对集合进行排序.sorted
在.map
product\u name x count
将这些字符串连接到整体订单摘要中.joined
var productsCount = [String: Int]()
for product in currentOrder.productsSold {
if let countedProduct = productsCount[product.name] {
productsCount[product.name] = productsCount[product.name]! + 1
} else {
productsCount[product.name] = 1
}
}
编辑
示例修改为使用产品
而不是字符串
作为字典键
首先更改产品
,以实现可散列
和可均衡
协议:
struct Product: Hashable {
var name: String
var price: Double
var hashValue: Int {
get {
return self.name.hashValue
}
}
static func ==(lhs: Product, rhs: Product) -> Bool {
return lhs.name == rhs.name
}
}
然后将密钥字典中的类型更改为产品
:
var productsCount = [Product: Int]()
for product in currentOrder.productsSold {
if let countedProduct = productsCount[product] {
productsCount[product] = productsCount[product]! + 1
} else {
productsCount[product] = 1
}
}
您可以使用计数集,例如使用swift进行黑客攻击:var spaceships=[“Serenity”、“Nostromo”、“Enterprise”]spaceships+=[“Voyager”、“Serenity”、“Star Destroyer”]spaceships+=[“Galactica”、“Sulaco”、“Minbari”]let countedSet=NSCountedSet(数组:spaceships)print(countedSet.count)(用于:“Serenity”)//2 print(用于:“Sulaco”)//1 NSCountedSet会更简单。谢谢,它成功了!现在,如果我需要每个产品的计数,我该怎么做?假设我需要:“product.name,product.color,product.price,…,productCount
struct Product: Hashable {
var name: String
var price: Double
var hashValue: Int {
get {
return self.name.hashValue
}
}
static func ==(lhs: Product, rhs: Product) -> Bool {
return lhs.name == rhs.name
}
}
var productsCount = [Product: Int]()
for product in currentOrder.productsSold {
if let countedProduct = productsCount[product] {
productsCount[product] = productsCount[product]! + 1
} else {
productsCount[product] = 1
}
}