Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift increment Int! not working

I understand how optionals work, but this is throwing me for a loop. I have a variable called num and I want to increment it, so I did the following:

var num:Int! = 0
num++             //ERROR - Unary operator ++ cannot be applied to an operand of type Int!

But for some reason Swift won't let me increment a force unwrapped Int, even though it is supposed to be treated like a regular Int with the capability for nil behind the scenes. So I tried the following, and it worked:

var num:Int! = 0
num = num + 1     //NO ERROR

However, based on the error message it gave me, I tried the following to make the increment operator still work:

var num:Int! = 0
num!++            //NO ERROR

My question is why does the first bit of code break, when the second and third bits of code don't? Also, since num is an Int!, shouldn't I be able to treat it like a regular Int? Lastly, since an Int! is supposed to be treated like a regular Int, how am I able to unwrap it in the third example? Thanks.

like image 1000
Adam Evans Avatar asked Aug 01 '15 15:08

Adam Evans


People also ask

How do you increment an INT in Swift?

How do you increment an integer in Swift? var num = 0. num = num + 1. num += 1.

Is there++ in Swift?

Swift provides an increment operator ++ and a decrement operator to increase or decrease the value of a numeric variable by 1. The operator with variables of any integer or floating-point type is used. The ++ and -- symbol is used as a prefix operator or postfix operator.

What is increment statement?

for example, the statement "i+=" means to increment the value of x by 1. Like, The statement "x-" means to decrement the value of x by 1.

How to use decrement?

The decrement operator is represented by two minus signs in a row. They would subtract 1 from the value of whatever was in the variable being decremented. The precedence of increment and decrement depends on if the operator is attached to the right of the operand (postfix) or to the left of the operand (prefix).


1 Answers

This error occurs with all inout parameters and should be considered a bug.

The usual way how inout parameters work is that their getter gets called once and their setter at least once. In this case the getter returns an Int!:

let num: Int! = 0
let num2 = num // is inferred to be of type Int!

so the signature of the getter/setter is not the same as the function/operator expects. But the compiler should implicitly unwrap the value if it gets assigned to an Int or passed like so:

var num3 = 0 // is of type Int
num3 = num // gets automatically unwrapped

// also with functions
func someFunc(i: Int) {}
someFunc(num) // gets automatically unwrapped

Sidenote:

Almost the same error occurs if you want to pass an Int to a function with an inout parameter of type Int!. But here it is obvious why this doesn't work (logically): The setter of an Int never takes a nil.

like image 106
Qbyte Avatar answered Oct 19 '22 23:10

Qbyte