简体   繁体   English

for循环项目的值不变

[英]Value Of for loop item is not changing

Below is my view controller. 下面是我的视图控制器。 I want to change background color of view depends on coming binary data. 我想更改视图的背景颜色取决于即将来临的二进制数据。 But my for loop sendingBit variable is showing always 0 value. 但是我的for循环sendingBit变量始终显示0值。 Value of sendingBit is not changing. 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)
                }
            }
 }

the issue is not related to the scheduled timer I'd say. 这个问题与我要说的预定计时器无关。 The Timer.scheduledTimer method makes sure that your code is running on your main thread (UIThread). Timer.scheduledTimer方法可确保您的代码在主线程(UIThread)上运行。 Stripping down your code to just the basics of starting the timer and setting the background shows that this is properly working. 将代码简化为仅启动计时器和设置背景的基础知识,这表明此方法正常工作。

Try to remove the timer and see where the code fails. 尝试删除计时器,看看代码在哪里失败。 It is probably somewhere in your color generation code. 它可能在您的颜色生成代码中。 Maybe you can start a new thread related to that as soon as you've found out where the issue is at exactly. 一旦找到问题的确切根源,也许您可​​以启动与此相关的新线程。

I like this syntax better than what you've got. 我喜欢这种语法胜过您所拥有的语法。 Seems to work for me in a playground. 似乎在操场上为我工作。

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")
   }
}

Might as well use the prettier pattern matching syntax sugar. 最好使用更漂亮的模式匹配语法糖。 Also, stringly typing a UIColor isn't a good idea in practice when you can just access the variable directly. 另外,实际上,直接输入变量时,字符串输入UIColor也不是一个好主意。

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

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