swift3 - Xcode 8 Beta 4 Swift 3 - "round"行为已更改

标签 swift3

我对 Double 有以下简单扩展,它在 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 开始,我在 return 中的 round 函数上收到“Cannot use mutating member on immutable value: 'self' is immutable” - 有人有任何线索吗?

最佳答案

这是由于与 the new rounding functions 的命名冲突造成的关于 FloatingPoint协议(protocol),round()rounded(),已从 Xcode 8 beta 4 开始添加到 Swift 3。

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

return Darwin.round(v) * divisor

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

return v.rounded() * divisor

关于swift3 - Xcode 8 Beta 4 Swift 3 - "round"行为已更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39761838/

相关文章:

ios - swift 3 Xcode 8.3 中的 HTTP 请求

ios - 如何更改参数以接受 swift 3 中的变量?

objective-c - 在创建 Objective C 到 Swift Bridge 时“在类型的对象上找不到属性”

ios - 在 CoreData ios swift3 中仅获取表的几个属性

iOS 11 UITableView isEditing 标志设置不正确

for-loop - swift 3 : how to write this for(;;) loop

swift - 使用 openTok 发送消息

ios - Xcode 8 : Archive build fails (for nested frameworks) with link errors. 常规构建编译成功

ios - 如何修复 JSON 未填充我的 UITableView 的问题?

swift - 在 Swift 3.0 中将以下 urlString 转换为编码的 urlString