Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Absolute value function abs() unavailable in Swift 3.0. Use abs(_:) free function? [closed]

I am converting a Swift iOS project to Swift 3.0 using Xcode 8 Beta 4. The absolute value function shows to be unavailable. Here is a code sample...

let myInt = -3
let myAbsoluteInt = abs(myInt)

The second line results in an error stating: 'abs' is unavailable: Please use the 'abs(_:)' free function.

Ideas?

like image 807
ninefifteen Avatar asked Aug 04 '16 15:08

ninefifteen


People also ask

How do you make a number positive in Swift?

The abs() function returns the absolute value of a number, which is a way of describing how far away from zero it is without thinking about whether it's positive or negative. It's most commonly used if you have a number that you need to be positive, because whether you pass 30 or -30 to abs() you get back 30.

What is fabs in Swift?

Returns the absolute value of each element in a vector.


2 Answers

The code you're showing us works for me in Xcode 8 beta 4.

I suppose you have another, different issue causing this misleading error message.

For example, it could be a conflict if you have imported other libraries.

A quick workaround would be to use the complete type:

let myAbsoluteInt = Swift.abs(myInt)
like image 143
Eric Aya Avatar answered Oct 18 '22 17:10

Eric Aya


You can try explicit Int as parameter as mentioned here it may be a bug : Ambiguous use of operator '-' in Swift with 'abs()'

let myInt:Int = -3
let myAbsoluteInt = abs(myInt)
like image 36
Alaeddine Avatar answered Oct 18 '22 17:10

Alaeddine