UILabel 无法正确更新

UILabel fails to update properly

我是初学者,正在 Xcode (Swift) 开发 iOS 应用程序。出于某种原因,我可以使用它的 属性 .text 在 UILabel 中设置文本,但如果我尝试更新它,标签将只显示我设置的最后一个值 (??)。例如,这是当我单击名为 myButton 的按钮并尝试在名为 Display 的标签中设置文本时执行的代码。结果是仅显示第四个值 ("Four")(在三个 2 秒延迟之后),因为 UILabel 在此之前保持空白。真正奇怪的是,如果我删除除第一行 (Display.text = "One") 以外的所有内容,它就会正常显示。就好像编译器删除了标签的最后一个 属性 设置之外的所有设置...感谢任何帮助!!

@IBAction func myButton(_ sender: Any)
{
    Display.text = "One"
    sleep(2)
    Display.text = "Two"
    sleep(2)
    Display.text = "Three"
    sleep(2)
    Display.text = "Four"
}

更新:这段代码只是一个例子,演示了我在实际程序中遇到的一个问题。 sleep() 调用仅用于模拟延迟。问题是我似乎无法将 UILabel 的文本设置两次。在我的代码中,我调用了一个需要时间执行的函数,所以我想在标签中显示 "Wait..." 直到它完成,然后我想在同一个标​​签中显示结果。但是运气不好...

更新 2:我在 Apple 文档中找到了这个:"You provide the content for a label by assigning either a NSString object to the text property, or an NSAttributedString object to the attributedText property. The label displays the most recently set of these properties." 最新的?好像是我的 problem.How 可以强制按需更新吗???

不要使用sleep它会阻塞主线程

let arr = ["one","two","three","four"]
var counter = 0
Timer.scheduledTimer(withTimeInterval:2.0, repeats:true) { timer in
    self.counter += 1
    if counter < self.arr.count {
       self.display.text = self.arr[counter]
    }
    else {
        timer.invalidate()
    }
}

或使用DispatchQueue.main.asyncAfter

arr.indices.forEach {
   DispatchQueue.main.asyncAfter(deadline: .now() + Double( [=11=] * 2) ) { 
      self.display.text = self.arr[[=11=]]
   }
}