Xcode 8 Beta 4 Swift 3 - “圆形"行为改变 [英] Xcode 8 Beta 4 Swift 3 - "round" behaviour changed

查看:19
本文介绍了Xcode 8 Beta 4 Swift 3 - “圆形"行为改变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下对 Double 的简单扩展,它在 Xcode 8 beta 3 之前的所有内容中都运行良好

I have the following simple extension to Double, which worked fine in everything up to Xcode 8 beta 3

public extension Double {
    public func roundTo(_ decimalPlaces: Int) -> Double {
        var v = self
        var divisor = 1.0
        if decimalPlaces > 0 {
            for _ in 1 ... decimalPlaces {
                v *= 10.0
                divisor *= 0.1
            }
        }
        return round(v) * divisor
    }
}

从 Beta 4 开始,我在返回的 round 函数上收到无法在不可变值上使用变异成员:'self' 是不可变的" - 有没有人有任何线索?

As of Beta 4, I am getting "Cannot use mutating member on immutable value: 'self' is immutable" on the round function in the return - has anyone got any clues?

推荐答案

这是由于与 新的舍入函数 FloatingPoint 协议,round()rounded(),它们已从 Xcode 8 beta 4 开始添加到 Swift 3.

This is due to a naming conflict with the new rounding functions on the FloatingPoint protocol, round() and rounded(), which have been added to Swift 3 as of Xcode 8 beta 4.

因此,您要么需要通过指定您指的是 Darwin 模块中的全局 round() 函数来消除歧义:

You therefore either need to disambiguate by specifying that you're referring to global round() function in the Darwin module:

return Darwin.round(v) * divisor

或者,更好的是,简单地使用新的舍入函数并在 v 上调用 rounded() :

Or, even better, simply make use of the new rounding functions and call rounded() on v:

return v.rounded() * divisor

这篇关于Xcode 8 Beta 4 Swift 3 - “圆形"行为改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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