1

I have a Swift file where I import Foundation and CoreGraphics, but at the place where I call abs(x) where x is a CGFloat, I get this warning:

abs is deprecated: Please use the abs(_) free function

What function should I use?

Thank you

Sulthan
  • 128,090
  • 22
  • 218
  • 270
Pop Flamingo
  • 3,023
  • 2
  • 26
  • 64
  • 1
    Where exactly are you calling `abs(x)`? Please provide a [mcve]. Sounds like you're calling `CGFloat`'s static `abs` method rather than the top-level `abs` function. Are you calling it at static scope in a `CGFloat` extension by any chance? – Hamish Apr 26 '17 at 16:03
  • It's giving no warning in xcode 8.2 let a: Double = 10.03 let value = abs(a) – Sahil Apr 26 '17 at 16:07
  • what version of Xcode are you using? – Sahil Apr 26 '17 at 16:37
  • 1
    @Hamish have a look at the api changes https://developer.apple.com/reference/swift/double/1538930-abs?changes=latest_minor . i guess this is a bug in xcode 8.0-8.1 – Sahil Apr 26 '17 at 16:58
  • @Hamish Yup that's excactly what I am doing, calling it in a static function extending CGFloat ! – Pop Flamingo Apr 26 '17 at 21:41

1 Answers1

4

That seems to be a compiler bug, if you are using abs(x) in a static context.

According to the comments you can work around the warning by using:

Swift.abs(x)
shallowThought
  • 19,212
  • 9
  • 65
  • 112
  • 3
    I wouldn't *really* consider it a bug – I would expect a call to `abs(_:)` in a static context on a type that has a static method `abs(_:)` to by default resolve to *that* method, rather than the top-level `abs(_:)`. But yes, using `Swift.` is the proper way to disambiguate in this case :) – Hamish Apr 26 '17 at 16:48
  • @Hamish Agree. I thought about writing more but was not really sure how this case is supposed to be handled. It's filed as an open bug though. – shallowThought Apr 26 '17 at 16:50