Xcode 8 Beta 4 Swift 3-“回合"行为改变 [英] Xcode 8 Beta 4 Swift 3 - "round" behaviour changed
问题描述
我对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屋!