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

查看:83
本文介绍了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函数上得到不能在不可变值上使用变异成员:'自我'是不可变的"-有人有任何线索吗?

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天全站免登陆