Update Label In While Loop Swift

后端 未结 1 1722
悲&欢浪女
悲&欢浪女 2021-01-16 19:34

Correct me if I\'m wrong, but when I try to update my label in a while loop it won\'t update it. Is the reason that the while loop runs too quickly for the label to update w

1条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-16 20:17

    Try Below Code, this will surely works.

     while (currentSize < fileSize) {
    
            dispatch_sync(dispatch_get_main_queue(), { () -> Void in
                downloadLabel.attributedText = NSAttributedString(string: "\(currentSize) kbps", attributes: [NSFontAttributeName: UIFont(name: "Arial", size: 20)!, NSForegroundColorAttributeName: UIColor.darkTextColor()])
                });
    
        }
    

    Just paste your UI update code to main thread as shown above. Reason : You can't update your UI from background thread.

    0 讨论(0)
提交回复
热议问题