Swift 3 迁移 - 双扩展舍入问题
Swift 3 Migration - Double Extension rounding issue
我正在将我们的代码库迁移到 Swift 3,我遇到了一个我无法解释或修复的编译问题。
我在 Double
扩展中有一个方法可以将 Double
舍入到一定数量的数字:
public func roundToPlaces(places: Int) -> Double {
let divisor = pow(10.0, Double(places))
return round(self * divisor) / divisor
}
例如:
12.34567.roundToPlaces(2)
应该 return 12.35
。但是,我遇到了此扩展中使用的 round
方法的编译问题。据说我 Cannot use mutating member on immutable value: 'self' is immutable
.
对这里发生的事情有什么想法吗?我该如何解决这个问题?
我已经解决了这个问题。将 round(self * divisor)
更改为 (self * divisor).rounded()
解决了编译问题。
我正在将我们的代码库迁移到 Swift 3,我遇到了一个我无法解释或修复的编译问题。
我在 Double
扩展中有一个方法可以将 Double
舍入到一定数量的数字:
public func roundToPlaces(places: Int) -> Double {
let divisor = pow(10.0, Double(places))
return round(self * divisor) / divisor
}
例如:
12.34567.roundToPlaces(2)
应该 return 12.35
。但是,我遇到了此扩展中使用的 round
方法的编译问题。据说我 Cannot use mutating member on immutable value: 'self' is immutable
.
对这里发生的事情有什么想法吗?我该如何解决这个问题?
我已经解决了这个问题。将 round(self * divisor)
更改为 (self * divisor).rounded()
解决了编译问题。