gpt4 book ai didi

ios - 如何计算类属性中的项目? swift 语言

转载 作者:行者123 更新时间:2023-11-30 12:51:08 24 4
gpt4 key购买 nike

方法应该计算类属性中所有项目的价格。不幸的是,我在编程方面的知识非常有限。任何帮助将非常感激。

提前致谢。

最佳答案

在 Swift3 中

使用减少

func priceBeforeDiscounts() -> Int {
return items.reduce(0) { (result, item) in
return result + item.priceInPence
}
}

使用for in循环

func priceBeforeDiscounts() -> Int {
var sum: Int = 0
for item in items {
sum += item.priceInPence
}
return sum
}

关于ios - 如何计算类属性中的项目? swift 语言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40958081/

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