使用reduce方法计算struct属性的总和

时间:2017-02-09 08:59:44

标签: swift3

我想计算学生lis的总分,但是我得到了这个错误。我的reduce方法实现有什么问题?

contentView.transform = CGAffineTransformMakeRotation(-M_PI);

这是代码:

Cannot convert value of type '(_, _) -> Int' to expected argument type '(Result, Student) -> Result'

2 个答案:

答案 0 :(得分:0)

应该是

let totalScore = studentsList.reduce(0) { $0 + $1.score }

let totalScore = studentsList.reduce(0) { $0.score + $1.score }

此处(0)的初始结果$0Result,在您的情况下为Int$1Student类型。

答案 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}