Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterate an array w/ explicit object type in Swift

Tags:

swift

I have an array:

let individualScores = [75, 43, 103, 87, 12]

And I iterate like this:

for score in individualScores {

}

However, is there a way to declare the object type explicitly? I think it would come in handy later w/ custom objects, or other reasons. Something like:

for Integer score in individualScores {

}
like image 926
Logan Avatar asked Jun 02 '14 22:06

Logan


3 Answers

When you type a variable, you do:

var score: Int

And you do the same in a loop:

for score: Int in individualScores {

}

It seems to be pretty consistent in that regard.

like image 78
Alex Wayne Avatar answered Oct 17 '22 16:10

Alex Wayne


yes its possible

let individualScores:Int[] = [75, 43, 103, 87, 12]

for score:Int in individualScores {

}
like image 5
Sam Avatar answered Oct 17 '22 17:10

Sam


Yes. You can explicitly specify the type if you wish.

let individualScores = [75, 43, 103, 87, 12]

for score: Int in individualScores {
    println(score)
}
like image 2
Mick MacCallum Avatar answered Oct 17 '22 18:10

Mick MacCallum