Swift 3 Migration - Double Extension rounding issu

2019-07-14 02:01发布

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.

compilation error image

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

1条回答
不美不萌又怎样
2楼-- · 2019-07-14 03:04

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

查看更多
登录 后发表回答