海河之家
2014-11-17 20:53
// 7-2 使用元组让函数返回多个值 import UIKit func maxminScores(scores:[Int]) ->(maxScore:Int,minScore:Int)? { if scores.isEmpty { return nil } var curmax = scores[0],curmin = scores[0] for score in scores[1..<scores.count] { curmax = max(curmax,score) curmin = min(curmin,score) } return (curmax,curmin) } //connect to the database var userScores:[Int]? = [12,990,572,3258,9999,1204] userScores = userScores ?? [] if let result = maxminScores(userScores!) { println("The maxnum user score is:\(result.maxScore)") println("The minnum user score is:\(result.minScore)") }
optional可选型确实很有特色,mutable可变性也是程序语言健壮性的一大特性。
好好好
玩儿转Swift
57181 学习 · 511 问题
相似问题