Swift increment Int! not working
Asked Answered
A

2

6

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.

Ascocarp answered 1/8, 2015 at 15:56 Comment(7)
Since you are initializing it right away to 0, it doesn't have to be optional. You can have var num = 0 without a need for initializersUrson
Thanks for the info, but regardless, why is my code above being weird?Ascocarp
@JackWu: Making it optional means you can set it to nil later if you have a reason to do so.Kerby
The postfix operator takes an "inout parameter", so this is essentially about the same problem: Passing an implicitly unwrapped variable into inout parameter doesn't compile.Republicanism
@Kerby I do know what an optional does. The question quite clearly expresses intention that num will never be set to nil.Urson
@MartinR That makes sense, but does that mean that binary operators don't take inout parameters, since my second code block works? It seems kind of weird that unary operators can't use optionals, but binary operators can use them.Ascocarp
@Aderis: No, binary operators take two operands and return a result. It is postfix func ++(inout x: Int) -> Int vs func +(lhs: Int, rhs: Int) -> Int. – But I don't know why the automatic unwrapping does not happen with IUOs.Republicanism
W
1

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.

Waterless answered 1/8, 2015 at 20:23 Comment(0)
K
-3

You are using wrong type Int!, use Int instead

Kktp answered 1/8, 2015 at 15:58 Comment(5)
I am using it in a ViewController, which doesn't have an init() method, and all my initialization is being done in viewDidLoad, so it has to be some sort of optional, and don't want to make it an Int? because then I would have to deal with question marks everywhere, and it always has a value after my viewDidLoadAscocarp
do the initialization in the declaration line var num = 0. Int is the default typeMicropathology
Thanks for the info, but regardless, why is my code with the optional being weird?Ascocarp
but your variable is initialized, why should you use optional? And Int! is not optional, Int? is optional. And in optional case num!++ is correct way to increment this variableKktp
This is not addressing the real issue here, i.e. "Why does ++ not work on Int!".Pelargonium

© 2022 - 2024 — McMap. All rights reserved.