Abs can be used to get the absolute value of a calculation that may have a positive or negative value. As an example without Abs we could go for…
var difference: Int
if currentValue > targetValue {
difference = currentValue - targetValue
} else if targetValue > currentValue {
difference = targetValue - currentValue
} else {
difference = 0
}
or…
var difference: Int = targetValue - currentValue
if difference < 0 {
difference = difference * -1
}
However, using the Abs function we can simply type…
let difference = abs(targetValue - currentValue)