[英]Swift - how can I make a while loop run in an app?
I am trying to build a metronome app, where when activated the app flashes a light and plays a sound in given intervals.我正在尝试构建一个节拍器应用程序,当激活该应用程序时,该应用程序会闪烁灯光并以给定的间隔播放声音。 However, I don't know where to put a while loop, as putting it inside the viewcontroller prompts an "expected declaration" error.
但是,我不知道在哪里放置 while 循环,因为将它放在 viewcontroller 中会提示“预期声明”错误。 The idea is just for something like:
这个想法只是为了:
while metronome_is_on = true {
//code that plays sound/flashes light with given delayed intervals
}
So, where can I run this loop in the app?那么,我在哪里可以在应用程序中运行这个循环呢? Or is there a better practice for this?
或者有更好的做法吗?
As I understand, you probably are new to Swift, so here it goes...据我了解,您可能是 Swift 的新手,所以就这样吧……
All of your code setup, goes inside the ViewController's viewDidLoad
.您所有的代码设置都在 ViewController 的
viewDidLoad
中。 viewDidLoad
is called the moment a ViewController is loaded into memory. viewDidLoad
在 ViewController 加载到 memory 时被调用。
To play a sound, check here;要播放声音,请在此处查看; How to play a sound using Swift?
如何使用 Swift 播放声音?
To run a block of code on certain intervals, check here;要在特定时间间隔运行一段代码,请在此处查看; https://www.hackingwithswift.com/articles/117/the-ultimate-guide-to-timer
https://www.hackingwithswift.com/articles/117/the-ultimate-guide-to-timer
As far your while-loop
is concerned, have in mind that to check for equality you need ==
.就您的
while-loop
而言,请记住,要检查是否相等,您需要==
。 =
is used for value assignment. =
用于赋值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.