xcode Swift - 如何知道循环何时结束?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31778352/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Swift - How to know when a loop has ended?
提问by dan martin
Please, forgive me as i'm verynew to swift, and programming in general.
请原谅我,因为我对 swift 和一般的编程非常陌生。
Believe me when I say I've tried hard to understand this, but I simply can't and would greatly appreciate any help.
当我说我已经努力理解这一点时,请相信我,但我根本无法理解,并且非常感谢任何帮助。
say I have this function:
说我有这个功能:
func loop() {
for var i=0; i<5; i++ {
println(i)
}
}
and I wanted to print to the logs "loop has finished" once this loop had completed and finished running, how would I do this? If i do:
并且我想在此循环完成并完成运行后将“循环已完成”打印到日志中,我该怎么做?如果我做:
func loop() {
for var i=0; i<5; i++ {
println(i)
println("loop has finished")
}
}
then "loop has finished" gets printed after every time i is incremented.
然后每次增加 i 后都会打印“循环已完成”。
I've tried reading into closures and completion handlers, but it's all going over my head at the moment and I don't really understand how i'd achieve the task above.
我试过阅读闭包和完成处理程序,但目前这一切都在我的脑海中,我真的不明白我是如何完成上述任务的。
If there's an angel that can show me how I'd complete my above example, I'd be in your debt.
如果有一个天使可以告诉我如何完成上面的例子,我会欠你的。
Thankyou for your patience with my currently pathetic knowledge!
感谢您对我目前可怜的知识的耐心!
回答by johnslay
To produce the same result as what others have posted but with basic closure syntax:
要产生与其他人发布的结果相同但具有基本闭包语法的结果:
func printFunction() {
println("loop has finished")
}
func loopWithCompletion(closure: () -> ()) {
for var i=0; i<5; i++ {
println(i)
}
closure()
}
This is how you call it:
你是这样称呼它的:
loopWithCompletion(printFunction)
Swift 3 Update:
斯威夫特 3 更新:
func printFunction() {
print("loop has finished")
}
// New for loop syntax and naming convention
func loop(withCompletion completion: () -> Void ) {
for i in 0 ..< 5 {
print(i)
}
completion()
}
Then call it like this:
然后像这样调用它:
loop(withCompletion: printFunction)
Or
或者
loop {
print("this does the same thing")
}
回答by ecatalano
Here is the loop you tried to implement:
这是您尝试实现的循环:
func loop() {
for var i=0; i<5; i++ {
println(i)
println("loop has finished")
}
}
The reason why "loop has finished" gets printed 5 times is because it is within the for-loop.
“循环已完成”被打印 5 次的原因是它在 for 循环内。
Anything in between your brackets will be run when the loop repeats (where I entered "enter code here)
当循环重复时,括号之间的任何内容都将运行(我在此处输入“在此处输入代码)
for var i=0; i<5; i++ {
enter code here
}
The way that loops work is, it will repeat until the condition is completed: i.e. i<5
. Your i variable starts at 0 as you stated in var i=0
, and every time the loop repeats, this number increases by 1, as stated in i++
循环的工作方式是,它会重复直到条件完成:即i<5
。您的 i 变量从 0 开始,如您在 中所述 var i=0
,每次循环重复时,此数字都会增加 1,如中所述i++
Once i
is notless than 5 (i is equal to 5 or greater than 5), then the code following your for-loop will be run.
一旦i
是不小于5(i是等于5或大于5个),然后按照你的for循环将要运行的代码。
So if you put your println("loop has finished") right after the forloop, you know it will run when the loop is completed, like this:
因此,如果您将 println("loop has finished") 放在 forloop 之后,您就会知道它会在循环完成时运行,如下所示:
func loop() {
for var i=0; i<5; i++ {
println(i)
}
println("loop has finished")
}
回答by Brett Wagner
func loop() {
for var i=0; i<5; i++ {
println(i)
}
println("loop has finished")
}
回答by CraftMcMatt
The reason why your code doesn't work is because you have the println()
statement inside the loop, like this:
您的代码不起作用的原因是因为您println()
在循环中有语句,如下所示:
func loop() {
for var i=0; i<5; i++ {
println(i)
println("loop has finished")
}
}
So it prints out "Finished" every time the loop loops.
因此,每次循环循环时,它都会打印出“已完成”。
To fix this, all you need to do is put the println()
statement after the loop, like so:
要解决此问题,您需要做的就是将println()
语句放在循环之后,如下所示:
func loop() {
for var i=0; i<5; i++ {
println(i)
}
println("loop has finished")
}
And voilà. Your app will now function properly!
瞧。您的应用程序现在将正常运行!
回答by SwiftNewling
Your print statement always prints out on every loop. Thats why you should put your that statement outside the foreach loop:
您的打印语句总是在每个循环中打印出来。这就是为什么你应该把你的那个语句放在 foreach 循环之外:
func loop() {
for i in 0...<5 {
print(i)
}
// it enters here when the loop is finished
print("loop has finished")
}