Add a delay to a for loop in swift

后端 未结 6 1576
感动是毒
感动是毒 2021-01-05 08:02

I have a coding \'issue\'.

I have a label, which text I want to change dynamically every 2 seconds. I\'ve done the following:

// WELCOME STRING ARRA         


        
6条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-05 08:30

    If you want to keep it all inline you can do this:

    var loop: ((Int) -> Void)!
    loop = { [weak self] count in
      guard count > 0 else { return }
      //Do your stuff
      DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
        loop(count - 1)
      }
    }
    loop(10) //However many loops you want
    

提交回复
热议问题