扩展双轮到Swift 3

Ari*_*ang 1 swift

我有以下扩展名

extension Double {
    func roundToPlaces(places:Int) -> Double {
        let divisor = pow(10.0, Double(places))
        return (self * divisor).rounded / divisor
    }
}
Run Code Online (Sandbox Code Playgroud)

当我将它更新到Swift 3时,它不起作用.我在这里尝试了解决方案,但我得到了

Binary operator '/' cannot be applied to operands of type '_' and 'Double' 错误

Cod*_*ent 5

你忘了一对括号:应该rounded()代替rounded:

extension Double {
    func roundToPlaces(places:Int) -> Double {
        let divisor = pow(10.0, Double(places))
        return (self * divisor).rounded() / divisor
    }
}
Run Code Online (Sandbox Code Playgroud)