我正在关注
this RW教程以了解Swift,并且我在以下函数声明的第一行遇到错误:
func returnPossibleTips() -> [Int: Double] { let possibleTipsInferred = [0.15,0.18,0.20] let possibleTipsExplicit:[Double] = [0.15,0.20] var retval = [Int: Double]() for possibleTip in possibleTipsInferred { let intPct = Int(possibleTip*100) retval[intPct] = calcTipWithTipPct(possibleTip) } return retval }
这些是错误:
解决方法
看起来你没有使用Swift的最新版本(beta 5),在第一版中没有数组的[Int]语法.
你可以更新Xcode或重写这段代码:
func returnPossibleTips() -> Dictionary<Int,Double> { let possibleTipsInferred = [0.15,0.20] let possibleTipsExplicit:Array<Double> = [0.15,0.20] var retval = Dictionary<Int,Double>() for possibleTip in possibleTipsInferred { let intPct = Int(possibleTip * 100) retval[intPct] = calcTipWithTipPct(possibleTip) } return retval }