For a loop in Swift

Suddenly, the for loop structure that I learned from the Apple documentation stopped working, it shows an error: expected declaration. Can someone tell me what the new syntax is?

let CirclePoints = 84

var circlePoint = 0

for circlePoint in 0..<CirclePoints {

}

This method also did not work:

for var circlePoint = 0; circlePoint < CirclePoints; circlePoint++ {

}
+4
source share
1 answer

Like others, your code works just fine on its own. If you get the expected error declaration, you can write code inside your class as follows:

class myClass{
let CirclePoints = 84

var circlePoint = 0

for circlePoint in 0..<CirclePoints {

}

}

You can declare and set variables in the scope of the class, but any other logic must go inside the methods. If you want the for loop to be executed when you instantiate the class, you can put it in the init method of the class.

class myClass{
    let CirclePoints = 84
    var circlePoint = 0

    init(){
        for circlePoint in 0..<CirclePoints {

        }
    }
}
+15
source

All Articles