想要实现类似于这样的效果:
var hasInserted:Bool = false for 。。。in 。。。。 { 。。。 if。。。 hasInserted = true } }
for循环中,当满足某个条件,想要直接退出for循环
搜:
swift for jump out
swift for in break
Break in a Loop StatementWhen used inside a loop statement, break ends the loop’s execution immediately, and transfers control to the first line of code after the loop’s closing brace (}). No further code from the current iteration of the loop is executed, and no further iterations of the loop are started.
->
所以在for循环中,直接用break:
var hasInserted:Bool = false for 。。。in 。。。。 { 。。。 if。。。 hasInserted = true break } }
即可。
转载请注明:在路上 » [已解决]swift跳出for循环