Swift Warning Variable Never Used - Please explain me

Hello,


I get a compiler warning at line 02 : Variable 'i' was never used; consider replacing with '_' or removing it


func test01 () {
    var i: Int
    for i in 1...10 {
        print (i)
    }
}


Why does the compiler consider that my variable "i" is never used, it is used at line 3 and 4 ?


If I remove the line 2 "var i: Int", the compiler is satisfied but not me.

I am an experimented programmer but new to swift.

I use to always declare my variable, and I expect the compiler to inform me if I use an undeclared variable to avoid typo in my coding.


Subsidiary question, where can I find in the documentation the meaning of compiler warnings or errors ?

I tried secondary click on warning --> Show Quick Help but got no results 😟


Thanks for your help.


M1ch3l

Replies

The point is you have 2 declarations of i:


first var i: Int


Then, you declare another one in for i ; it is a local variable for the loop, not the one you declared before.

And it disappears at the end of the loop.

That's what is called local scoping.


Make the test:


func test01 () {
    var i: Int
    for i in 1...10 {
        print  ( i )
   }
   print( i )     // That's where you get an error message
}

You get an error message :

Variable 'i' used before being initialized



Try this:

func test01 () {
    var i: Int = 100
    for i in 1...10 {
        print ("loop i", i)
    }
    print("final i", i)
}
test01 ()


You get:

loop i 1

loop i 2

loop i 3

loop i 4

loop i 5

loop i 6

loop i 7

loop i 8

loop i 9

loop i 10

final i 100


That shows well that the var i is not the one used in the loop


EDITED: it missed a line in second code snippet

Note, just for understanding declaration, that 'for i' is equivalent to

for var i in 0...10 {
     print(i)
}

which illustrated that a new var declaration is done.

(This works, but is a bad programming ; you'll get a warning: Variable 'i' was never mutated; consider removing 'var' to make it constant)

In addition, you must not force change the value of loop index inside the loop

As already explained, in Swift, for-statement always declares its loop-control variable.

So, your code is similar to this code in C:

    int i; //<- Unused variable 'i'
    for( int i = 1; i <= 10; ++i ) {
        printf("%d\n", i);
    }


always declare my variable

A good habit, and Swift forces us to declare all the variables.

If you want to represent that your for-statement declares a variable explicitly, you can write something like this.

    for i: Int in 1...10 {
        print (i)
    }

(Please remember even if you omitted `: Int`, Swift infers the type as Int and declares `i` as Int.)


where can I find in the documentation the meaning of compiler warnings or errors ?

Nowhere. You can write a feature request to improve documentation through Apple's bug reporter.

One more point:


the i you declare in for i, hides the previous i you declare before.

When you run

func test01 () {
    var i: Int
    for i in 1...10 {
        print ( i )
    }
}

you get

1

2

etc…

And in fact, var i was never used. It is hidden (in terms of scope) by the for i


if you now run

func test01 () {
    var i: Int
    for _ in 1...10 {
        print ( i )
    }
}

you get the compiler error of using i before initialization: Variable 'i' used before being initialized


You can get an even clearer illustration with this 3rd snippet:

if you now run

func test01 () {
    var i: Int = 0
    for _ in 1...10 {
        print ( i )
    }
}

Now you get

0

0

0

// 10 times

because var i is no more hidden as you loop with for _ (would be the same if you looped with for j )