Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does max doesn't work in CGFloat extension in Swift?

Tags:

swift

swift2

Anybody knows what this doesn't work (Static member min cannot be used on instance of Type CGFloat. in the extension.

extension CGFloat {
    mutating func normalize() {
        self = min(max(CGFloat(0), self), CGFloat(1))
   }
}

while this works

let f: CGFloat = CGFloat(0.4)
let maxValue = max(f, 1)
like image 563
skittle Avatar asked Sep 10 '15 16:09

skittle


1 Answers

You can get this to work by specifying Swift.min and Swift.max as such:

extension CGFloat {
    mutating func normalize() {
        self = Swift.min( Swift.max(CGFloat(0), self), CGFloat(1))
    }
}

when using just min and max it is unsure if you mean CGFloat.min or Swift.min

like image 172
utahwithak Avatar answered Oct 15 '22 16:10

utahwithak