I'm trying to learn Swift and am looking at an old Generic example that worked in Swift 2
func increment<T: Strideable>(number: T) -> T {
return number + 1
}
Now in Swift 4 it complains
'+' is unavailable: Please use explicit type conversions or Strideable methods for mixed-type arithmetics
Why am I getting this error and what I am doing wrong?
Rather than using the +
operator, you can simply use Strideable.advanced(by:)
.
func increment<T: Strideable>(number: T) -> T {
return number.advanced(by: 1)
}
increment(number: 2) //returns 3
increment(number: 2.5) //returns 3.5
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With