Swift 3 Migration - Double Extension rounding issue

不打扰是莪最后的温柔 提交于 2019-12-23 22:12:09

问题


I'm migrating our codebase to Swift 3 and I've come across a compilation issue that I can't explain or fix.

I have a method in a Double extension that rounds the Double to a certain number of digits:

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

For example: 12.34567.roundToPlaces(2) should return 12.35. However, I'm getting a compilation issue for the round method used in this extension. It's saying that I Cannot use mutating member on immutable value: 'self' is immutable.

Any ideas on what's going on here? How do I fix this issue?


回答1:


I've fixed the issue. Changing round(self * divisor) to (self * divisor).rounded() resolved the compilation issue.



来源:https://stackoverflow.com/questions/39512671/swift-3-migration-double-extension-rounding-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!