Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
530 views
in Technique[技术] by (71.8m points)

swift3 - Xcode 8 Beta 4 Swift 3 - "round" behaviour changed

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
    }
}

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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

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

return v.rounded() * divisor

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...