ios – 字典作为函数返回类型

前端之家收集整理的这篇文章主要介绍了ios – 字典作为函数返回类型前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在关注 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
}
原文链接:https://www.f2er.com/iOS/333636.html

猜你在找的iOS相关文章