首页 > 解决方案 > Swift等效于while循环与python中的else

问题描述

我有以下用 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
                        
                    }
                }
            }
        }

所有内部接触都始于 Spritekit。但由于某种原因,我在尝试编译时遇到了两个错误。

  1. 闭包表达式未使用,在它说 else 的那一行。不知道为什么,因为表达式没有被使用,因为里面有代码。
  2. 未标记的中断只允许在循环或开关内部,需要标记的中断才能退出以及 if 或 do。我在这里感到困惑-未标记和标记的中断之间有什么区别?而 else 语句是响应 while 循环,如果 playercardvals 的总和大于 20

标签: swift

解决方案


Swift中没有while ... else ...,你将不得不重构你的逻辑。

此外,您不想在touchesBegan您的应用程序内部循环将停止响应!


推荐阅读