我想计算学生lis的总分,但是我得到了这个错误。我的reduce方法实现有什么问题?
contentView.transform = CGAffineTransformMakeRotation(-M_PI);
这是代码:
Cannot convert value of type '(_, _) -> Int' to expected argument type '(Result, Student) -> Result'
答案 0 :(得分:0)
应该是
let totalScore = studentsList.reduce(0) { $0 + $1.score }
不
let totalScore = studentsList.reduce(0) { $0.score + $1.score }
此处(0)
的初始结果$0
为Result
,在您的情况下为Int
且$1
为Student
类型。
答案 1 :(得分:0)
第一批studentList初始化
var studentsList: [Student] = [] //or some list
它应该
let totalScore = studentsList.reduce(0) {$0.0 + $0.1.score}
而不是
let totalScore = studentsList.reduce(0) {$0.score + $1.score}