我有一个名为items1的数组,它的成员是一个结构Team1,它有两个成员id: UUID()和Team1: Int。我想做一个函数来计算数组的舍入成员之和。有没有人能帮我弄明白我在下面的代码中做错了什么:
import Foundation
struct vrb {
static var items1 = [
Team1(id: UUID(), round: 14),
Team1(id: UUID(), round: 20),
Team1(id: UUID(), round: 24),
]
static var total1: Int = 0
}
func Total() -> Int {
var sum = vrb.items1[round.reduce(0, +)]
}
struct Team1: Identifiable {
var id: UUID
var round: Int
}发布于 2021-04-18 23:54:29
你已经很接近了。你要找的是:
func total() -> Int {
vrb.items1.map(\.round).reduce(0, +)
}将items1 ("transform each element of")映射到它的.round值,然后对这些值求和。
(请注意,函数、方法和变量应该以小写字母开头,而结构等类型应该以大写字母开头。这使得其他Swift开发人员更容易理解您的代码。)
https://stackoverflow.com/questions/67150366
复制相似问题