+ 2
I didn't understand the last line of the hint .
can any one help me to make clear for the meaning of the last line of the hint given in swift programming for loop... please help me out
4 Antworten
+ 7
Now I understand what you're talking about.
Given this code:
for var index = 0; index < 3; ++index {
print("index is \(index)")
}
What the hint is trying to explain is that the value of variable "index" can only be accessed inside the for loop because it's a local variable. When the loop ends the variable "index" is destructed and loses the value.
If you want to access the value of the variable when loop ends, you should declare it outside the loop.
Like this:
var index;
for index = 0; index < 3; ++index {
print("index is \(index)")
}
When loop ends you can retrieve the value because the variable was created outside the loop and it's not destructed.
+ 5
Post the link to the page where the hint appears.
0
Constants and variables in the initialization expression (such as var index = 0) are only valid within the for loop itself. To retrieve the final value of index after the loop ends, you must declare index before the loop begins.
0
the post is the hint that could not understand.please help me to understand it...