【发布时间】:2015-07-26 23:11:05
【问题描述】:
浏览 swift 2.0 文档并尝试练习一些我在 C++ 中学到的东西。其中之一是能够修改我的元素内部的数组元素,而我在 swift 中无法做到这一点。
var scoreOfStudents = [86, 93, 68, 78, 66, 87, 80]
func returnScoresWithCurve (inout scoresOfClass : [Int]) -> [Int] {
for var score in scoresOfClass {
if score < 80 {
score += 5
}
}
return scoresOfClass
}
不知道我的错误是什么,因为在 for-in 循环中,正在添加小于 80 的分数,但在我传递的数组中没有被修改。还想知道如何使用嵌套函数而不是 for-in 循环来做同样的事情。
【问题讨论】: