簡體   English   中英

for循環項目的值不變

[英]Value Of for loop item is not changing

下面是我的視圖控制器。 我想更改視圖的背景顏色取決於即將來臨的二進制數據。 但是我的for循環sendingBit變量始終顯示0值。 sendingBit值沒有改變。

            class ViewController: UIViewController {

            @IBOutlet weak var backgroundView: UIView!

            var timer = Timer()
            var condition = "black"
            var count = 0

 var bitEmementsArray = [0,1,1,0,0,1,0,0,0,1,0,1,1,0,0,1,0,0,0,1,0,1,1,0,0,1,0,0,0,1,0,1,1,0,0,1,0,0,0,1,0,1,1,0,0,1,0,0,0,1,1,0,0,1,0,0,0,1,0,1,1,0,0,1,0,0,0,1,0,1,1,0,0,1,0,0,0,1,0,1,1,0,0,1,0,0,0,1,0,1,1,0,0,1,0,0,0,1,1,0,0,1,0,0,0,1,0,1,1,0,0,1,0,0,0,1,0,1,1,0,0,1,0,0,0,1,0,1,1,0,0,1,0,0,0,1,0,1,1,0,0,1,0,0,0,1,1,0,0,1,0,0,0,1,0,1,1,0,0,1,0,0,0,1,0,1,1,0,0,1,0,0,0,1,0,1,1,0,0,1,0,0,0,1,0,1,1,0,0,1,0,0,0,1]            var currentColor = "black"

            override func viewDidLoad() {
                super.viewDidLoad()
                navigationController?.navigationBar.isHidden = true
                timer = Timer.scheduledTimer(timeInterval: 0.99, target: self, selector: #selector(animation), userInfo: nil, repeats : true)

            }

            @objc func animation()
            {
    for sendingBit in bitEmementsArray {
                print(sendingBit)
                if currentColor == "black" && sendingBit == 0 {
                    backgroundView.backgroundColor = UIColor.gray
                    currentColor = "grey"
                    break;
                }
                else if currentColor == "black" && sendingBit == 1 {
                    backgroundView.backgroundColor = UIColor.white
                    currentColor = "white"
                    break;
                }
                else if currentColor == "grey" && sendingBit == 0 {
                    backgroundView.backgroundColor = UIColor.black
                    currentColor = "black"
                    break;
                }
                else if currentColor == "grey" && sendingBit == 1 {
                    backgroundView.backgroundColor = UIColor.white
                    currentColor = "white"
                    break;
                }
                else if currentColor == "white" && sendingBit == 0 {
                    backgroundView.backgroundColor = UIColor.black
                    currentColor = "black"
                    break;
                }
                else if currentColor == "white" && sendingBit == 1 {
                    backgroundView.backgroundColor = UIColor.gray
                    currentColor = "grey"
                    break;
                }

            }
                count += 1
                print(count)

                if count == bitEmementsArray.count + 1
                {
                    count = 0
                    timer.invalidate()
                    AudioServicesPlayAlertSound(kSystemSoundID_Vibrate)
                    dismiss(animated: false, completion: nil)
                }
            }
 }

這個問題與我要說的預定計時器無關。 Timer.scheduledTimer方法可確保您的代碼在主線程(UIThread)上運行。 將代碼簡化為僅啟動計時器和設置背景的基礎知識,這表明此方法正常工作。

嘗試刪除計時器,看看代碼在哪里失敗。 它可能在您的顏色生成代碼中。 一旦找到問題的確切根源,也許您可​​以啟動與此相關的新線程。

我喜歡這種語法勝過您所擁有的語法。 似乎在操場上為我工作。

import UIKit

var backgroundView = UIView()
backgroundView.backgroundColor = .black

var bitEmementsArray = [0,1,1,0,0,1,0,0,0,1,0,1,1,0,0,1,0,0,0,1,0,1,1,0,0,1,0,0,0,1,0,1,1,0,0,1,0,0,0,1,0,1,1,0,0,1,0,0,0,1,1,0,0,1,0,0,0,1,0,1,1,0,0,1,0,0,0,1,0,1,1,0,0,1,0,0,0,1,0,1,1,0,0,1,0,0,0,1,0,1,1,0,0,1,0,0,0,1,1,0,0,1,0,0,0,1,0,1,1,0,0,1,0,0,0,1,0,1,1,0,0,1,0,0,0,1,0,1,1,0,0,1,0,0,0,1,0,1,1,0,0,1,0,0,0,1,1,0,0,1,0,0,0,1,0,1,1,0,0,1,0,0,0,1,0,1,1,0,0,1,0,0,0,1,0,1,1,0,0,1,0,0,0,1,0,1,1,0,0,1,0,0,0,1]

for sendingBit in bitEmementsArray {
   switch (sendingBit, backgroundView.backgroundColor) {
   case (0, UIColor.black):
      backgroundView.backgroundColor = .gray
   case (1, UIColor.gray):
      backgroundView.backgroundColor = .black
   default:
      print("default")
   }
}

最好使用更漂亮的模式匹配語法糖。 另外,實際上,直接輸入變量時,字符串輸入UIColor也不是一個好主意。

暫無
暫無

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

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