2016-09-15 37 views
2

我正在将我们的代码库迁移到Swift 3,并且遇到了一个我无法解释或修复的编译问题。Swift 3 Migration - Double Extension舍入问题

我有一个Double扩展了一种方法,舍入Double到一定的位数:

public func roundToPlaces(places: Int) -> Double { 
    let divisor = pow(10.0, Double(places)) 
    return round(self * divisor)/divisor 
} 

例如: 12.34567.roundToPlaces(2)应返回12.35。但是,我收到了此扩展中使用的round方法的编译问题。这是说我Cannot use mutating member on immutable value: 'self' is immutable

compilation error image

在这里发生了什么任何想法?我如何解决这个问题?

+2

可能重复[Xcode 8 Beta 4 Swift 3 - “round”行为已更改](http://stackoverflow.com/questions/38767635/xcode-8-beta-4-swift-3-round-behaviour-changed ) –

+0

不应该舍去'12.34567'返回'12.35'? – Koen

+0

我的错误...是的,12.35。现在编辑... – Jake

回答

10

我已经解决了这个问题。将round(self * divisor)更改为(self * divisor).rounded()解决了编译问题。

+2

我必须这样做:(自我*除数).round()括号 –