简体   繁体   English

Swift 等价于 python 中的 while 循环和 else

[英]Swift equivalent for while loops with else in python

I have the following code written in Swift我有以下用 Swift 编写的代码

        if current_game_state == game_state.in_game {
            while player_card_values.sum() <= 20 {
                for touch: AnyObject in touches {
                    let point_of_touch = touch.location(in: self)
                    if hit_label.contains(point_of_touch){
                        spawn_card()
                        print("Hit Button Pressed")
                    }
                }
            }
            else {
                if self.player_card_values.sum() == 21 {
                    print("Player has received BJ!")
                    self.player_status = person_status.stand
                    break
                }
            }
                for touch: AnyObject in touches {
                    let point_of_touch = touch.location(in: self)
                    if stand_label.contains(point_of_touch){
                        print("Stand Button Pressed")
                        player_status = person_status.stand
                        
                    }
                }
            }
        }

All inside touchesBegan in Spritekit.所有内部接触都始于 Spritekit。 But for some reason, I am getting two errors when trying to compile this.但是出于某种原因,我在尝试编译它时遇到了两个错误。

  1. Closure expression is unused, on the line where it says else.闭包表达式未使用,在它说 else 的那一行。 Not sure why because the expression is not unused, as there is code inside it.不知道为什么,因为该表达式未被使用,因为其中有代码。
  2. Unlabeled break is only allowed inside a loop or a switch, a labeled break is required to exit and if or a do.未标记的中断只允许在循环或开关内,标记的中断需要退出和 if 或 do。 I'm confused here-- what's the difference between an unlabeled and labeled break?我在这里很困惑 - 未标记和标记的中断之间有什么区别? And the else statement is in response to the while loop, if the sum of the playercardvals is greater than 20并且 else 语句响应 while 循环,如果 playercardvals 的总和大于 20

There is no while ... else ... in Swift, you will have to refactor your logic.没有while ... else ...在 Swift 中,您将不得不重构您的逻辑。

Also, you do not want to loop inside touchesBegan your app will stop responding!此外,您不想在touchesBegan内循环touchesBegan您的应用程序将停止响应!

Closure expression is unused, on the line where it says else.闭包表达式未使用,在它说 else 的那一行。 Not sure why because the expression is not unused, as there is code inside it.不知道为什么,因为该表达式未被使用,因为其中有代码。

You have else right after where your while block ends.在你的while块结束之后,你还有else It makes sense after where if / else if blocks end, not after while .在 where if / else if块结束后才有意义,而不是在while之后。


Unlabeled break is only allowed inside a loop or a switch, a labeled break is required to exit and if or a do.未标记的中断只允许在循环或开关内,标记的中断需要退出和 if 或 do。

Your else block that contains the break is not a part of the while body.包含break else块不是while主体的一部分。


You need to regroup your code, you have misplaced } in your code and it is not making sense to the compiler.您需要重新组合您的代码,您在代码中放错了}并且它对编译器没有意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM