簡體   English   中英

Swift 等價於 python 中的 while 循環和 else

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

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

沒有while ... else ...在 Swift 中,您將不得不重構您的邏輯。

此外,您不想在touchesBegan內循環touchesBegan您的應用程序將停止響應!

閉包表達式未使用,在它說 else 的那一行。 不知道為什么,因為該表達式未被使用,因為其中有代碼。

在你的while塊結束之后,你還有else 在 where if / else if塊結束后才有意義,而不是在while之后。


未標記的中斷只允許在循環或開關內,標記的中斷需要退出和 if 或 do。

包含break else塊不是while主體的一部分。


您需要重新組合您的代碼,您在代碼中放錯了}並且它對編譯器沒有意義。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM