Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterating over a collection in Swift: var vs. let

Tags:

xcode

swift2

I have a method that iterates over an array and call other method with every element as argument. If I declare this method as:

func didFinishedListFiles(files: [FileModel]) {
    for var fileData in files {
        self.downloadSingleFile(NSUUID(UUIDString: fileData.uuid!)!);
    }
}

Xcode shows a warning:

Variable 'fileData' was never mutated; consider changing to 'let' constant

But if I change var to let:

func didFinishedListFiles(files: [FileModel]) {
    for let fileData in files {
        self.downloadSingleFile(NSUUID(UUIDString: fileData.uuid!)!);
    }
}    

Xcode shows an error:

'let' pattern cannot appear nested in an already immutable context

How is a correct way to implement it without any warnings/errors?

like image 632
Dominik Palo Avatar asked Nov 13 '15 16:11

Dominik Palo


People also ask

What is the difference between let and Var in Swift?

This posts presents an overview of let and var, and the differences between let and var in Swift: The var keyword in Swift allows you to create a mutable variable. A mutable variable can be initialized more than once. The let keyword in Swift allows you to create immutable variables.

How do you iterate over an array in Swift?

The for loop might be the most well-known method for iteration over all programming languages. It’s also known as the for-in loop in Swift. This example iterates over an array of cities, also known as a collection in Swift. This example iterates over a dictionary. Here, it prints out the age of each person.

How to create a variable in Swift?

var is the only way to create a variables in swift. var doesn't mean dynamic variable as in the case of interpreted languages like javascript. For example, In this case, the type of variable name is inferred that name is of type String, we can also create variables by explicitly defining type, for example

What is the use of let in Swift?

The let keyword in Swift allows you to create immutable variables. An immutable variable can be initialized only once and acts as a constant. What is the difference between let and var in Swift? A var variable can be changed after it is initialized. However, a let variable cannot be changed after it is initialized.


1 Answers

The for-in pattern implicitly uses a constant binding (in the scope it creates. That is, your fileData binding is automatically a local let, and therefore constant for each pass through the loop.

So the following:

for fileData in files { /*...*/ }

...is equivalent to :

var index = 0
while index < files.count {
    let fileData = files[index]
    //...
    index += 1
}

You'd want to add var to the for-in binding only when you want to mutate that binding -- that is, if it's an object reference that you want to be able to point at something else during a single pass through the loop, or a value type that you want to be able to change. But it doesn't look like you're doing either of those things, so using var for this binding would be superfluous here.

(Swift 3 got rid of a lot of the places where you could make implicitly immutable bindings mutable, but left for var as an exception — it's still possible if you want to change something during a loop.)

like image 82
rickster Avatar answered Oct 09 '22 15:10

rickster