繁体   English   中英

如何每天更新UILabel?

[英]How to update UILabel daily?

所以我只想每天更新一次报价,但是现在每次启动时都会更新? 感谢您的帮助!

let tasks = URLSession.shared.dataTask(with: URL(string: "https://talaikis.com/api/quotes/random/")!) { (data, response, error) in
        if error != nil {
            print("error")
        } else {
            if let content = data {
                do {
                    let Json = try JSONSerialization.jsonObject(with: content, options: JSONSerialization.ReadingOptions.mutableContainers) as AnyObject
                    if let data = Json as? [AnyHashable:Any] {

                        if let quote = data["quote"], let cat = data["cat"], let author = data["author"] as? String {

                            print(cat)

                            DispatchQueue.main.async {
                                    self.myLabel.text = "\(quote)"
                                    self.authorLabel.text = "\(author)"
                                }

                        }
                    }
                } catch {

                }
            }
        }
    }
    tasks.resume()

1-每次检索后缓存当前日期的时间戳(以秒为单位)

2-获取当前日期的时间戳(以秒为单位)

3-比较缓存是否为nil

if ( current - cached ) > 86400 { // 86400  = 24 * 60 * 60 <<<<< day seconds 
   // update 
}

暂无
暂无

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

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