在Swift中循环 [英] For loop in Swift

查看:135
本文介绍了在Swift中循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

突然间,我在苹果文档中学到的for循环结构停止工作,它显示一个错误:预期的声明。任何人都可以告诉我什么是新的语法?

 让CirclePoints = 84 

var circlePoint = 0

在0 ..< CirclePoints {

}

这种方式也没有't work:

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

}


解决方案

就像其他人说的那样,你的代码可以正常工作。如果你得到错误预期的声明,你可能已经写在你的类体内的代码如下:

$ p
让CirclePoints = 84

var circlePoint = 0

for circlePoint in 0 ..< CirclePoints {

}




$ b $ p
$ b

你可以在类的范围内声明和设置变量,但是所有其他变量逻辑必须走进方法。如果您希望在创建类的实例时执行for循环,则可以将其放在类的init方法中。

  class myClass {
让CirclePoints = 84
var circlePoint = 0

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

}
}
}


Suddenly the for loop structure I learned at Apple's documentation stopped working, it shows an error: Expected declaration. Can anyone tell me what's the new syntax?

let CirclePoints = 84

var circlePoint = 0

for circlePoint in 0..<CirclePoints {

}

This way also didn't work:

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

}

解决方案

Like the others have said, your code works fine on its own. If you're getting the error expected declaration, you may have written code inside your class body like this:

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 all other logic must go inside methods. If you want that for loop to execute when an instance of the class is created, you can put it in the init method for the class.

class myClass{
    let CirclePoints = 84
    var circlePoint = 0

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

        }
    }
}

这篇关于在Swift中循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆