Swift 3迁移 - 双重延伸四舍五入问题 [英] Swift 3 Migration - Double Extension rounding issue

查看:223
本文介绍了Swift 3迁移 - 双重延伸四舍五入问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将我们的代码库迁移到Swift 3,我遇到了一个无法解释或解决的编译问题。



我有一个 Double 扩展名中的方法,将 Double 到一定数量的数字:

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

例如:
12.34567.roundToPlaces(2)应该返回 12.35 。但是,我收到此扩展中使用的方法的编译问题。这是说我不能使用mutation成员不可变值:'self'是不可变的





有关这里发生了什么的想法?如何解决这个问题?

解决方案

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

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?

解决方案

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

这篇关于Swift 3迁移 - 双重延伸四舍五入问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆