我在随机创建的文本文件中读取
hard toffee 10
hard toffee 20
...
chewy gum 40
soft marshmallow 20
hard toffee 30
soft marshmallow 40我创建了一个糖果/糖果对象数组,并将其存储如下:
var candyArray = [
Candy(consistency: "hard", type: "toffee", cost: 10),
...
Candy(consistency: "soft", type: "marshmellow", cost: 40)]可以通过其属性访问每个对象:
print(\(candyArray[0].type))
// prints toffeee我想遍历这个数组,如果一致性很难,我想要将成本+=到一个用于存储硬糖成本之和的变量。我也想对其他一致性做同样的处理,然后比较它们,看看哪一个在总结时有最大的成本。任何帮助都将是非常感谢的。到目前为止,我的情况如下:
struct Candy {
var consistency: String
var type: String
var cost: Double
init(consistency: String, type: String, cost: Double) {
self.consistency = consistency
self.type = type
self.cost = cost
}
}
var candyArray = [
Candy(consistency: "hard", type: "toffee", cost: 40),
Candy(consistency: "hard", type: "toffee", cost: 5),
Candy(consistency: "hard", type: "toffee", cost: 5),
Candy(consistency: "soft", type: "marshmallow", cost: 30),
Candy(consistency: "soft", type: "marshmallow", cost: 35),
Candy(consistency: "chewy", type: "gum", cost: 35)
]
print("\(candyArray[0].type)")
var x = 0
var largestValue = 0.0
var tempValue = 0.0
var currentConsistency = candyArray[x].consistency
var mostExpensiveConsistency = ""
while (x < candyArray.count){
if (currentConsistency == candyArray[x].consistency) {
tempValue += candyArray[x].cost
} else if (currentConsistency != candyArray[x].consistency) {
tempValue = 0
currentConsistency = candyArray[x].consistency
}
if (tempValue > largestValue) {
largestValue = tempValue
mostExpensiveConsistency = currentConsistency
}
x+=1
}
print(" largest value: \(largestValue) and most expensive consistency: \(mostExpensiveConsistency)")当一致性类型没有像前面提到的文本文件那样排序时,代码就不能工作了。我正在考虑创建一个2d数组或一个字典,并将一致性存储为键,并将和存储为每个一致性的值,以便如果一致性再次出现,我可以将其添加到先前存储在数组/字典中的和中。我希望我说的有道理。我只是想知道是否有更快的方法。
发布于 2018-02-26 18:03:08
您可以使用Array.reduce(into:)创建一个Dictionary,其键是一致性,值是具有一致性的糖果成本之和。然后,只需在max(by:)上调用Dictionary,就可以找到最昂贵的一致性类型。
let candiesByConsistency = candyArray.reduce(into: [String:Double](), { accumulatedResults, current in
accumulatedResults[current.consistency, default: 0] += current.cost
})
let mostExpensiveConsistency = candiesByConsistency.max(by: { $0.value < $1.value })给定示例数组中的candiesByConsistency值将为
“软”:65,“硬”:50,“嚼”:35
而mostExpensiveConsistency将是
(键"soft",值65)
发布于 2018-02-28 16:17:14
Swift 4的字典初始化器可以为您完成大部分工作。
例如:
let costs = Dictionary(candyArray.map{($0.consistency,$0.cost)}, uniquingKeysWith:+)
let highest = costs.max{$0.value < $1.value} // ("soft",65)
let hardCost = costs["hard"] // 50发布于 2018-02-26 18:02:52
Swift在集合中提供了一些很好的函数来简化这类任务:map、filter和reduce。
例如,您可以通过以下方式获得硬糖的总成本:
let hardCost = candyArray.filter{ $0.consistency == "hard" }.map{ $0.cost }.reduce(0, +)这样做的目的如下:
filter:返回一个只包含与指定条件匹配的项的数组(consistency == "hard")
map:只返回来自filter结果的成本数组
reduce:通过对输入数组的所有元素执行操作(在本例中为+)来聚合单个结果
您可以对每种类型的一致性执行相同的过程,也可以编写一个扩展方法,该扩展方法以您想要的一致性的名称命名。
extension Array where Element == Candy {
func costOf(consistency: String) {
candyArray.filter{ $0.consistency == consistency }.map{ $0.cost }.reduce(0, +)
}
}然后像这样使用它来获得每个一致性的值:
let hardCost = candyArray.costOf(consistency: "hard")
let softCost = candyArray.costOf(consistency: "soft")
let chewyCost = candyArray.costOf(consistency: "chewy")https://stackoverflow.com/questions/48994103
复制相似问题