简体   繁体   中英

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. Value of sendingBit is not changing.

            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). 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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