应用程序上的日常新内容 [英] Everyday new content on Application
本文介绍了应用程序上的日常新内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个引号列表.我想要一个每 24 小时更改一次新报价的标签.我怎样才能做到这一点?比如我如何管理一天?
I have a list of quotes. I want to have a label that changes every 24H to a new Quote. How can I do this? Like how can I manage a day?
我知道我可以只使用 API,但我想使用自己的引号,而且我还不能自己创建 API.
I know I could just use an API but I want to use my own quotes and I am not able to create an API on my own yet.
推荐答案
假设你有一个引号数组:
Supposing you have an array of quotes:
let numberOfQuotes = 3
let quotes = ["quote a", "quote b", "quote c"]
override func viewDidLoad() {
_ = Timer.scheduledTimer(timeInterval: TimeInterval(60),
target: self,
selector: #selector(self.updateQuote),
userInfo: nil,
repeats: true)
}
func updateQuote() {
let lastUpdate = UserDefaults.standard.object(forKey: "lastUpdate") as? Date
if lastUpdate != nil {
let date1:Date = Date() // Same you did before with timeNow variable
let date2: Date = Date(timeIntervalSince1970: lastUpdate)
let calender:Calendar = Calendar.current
let components: DateComponents = calender.dateComponents([.year, .month, .day, .hour, .minute, .second], from: date1, to: date2)
// you can use components month, hour, second.... to update your message, in your case, we will day
if components.day! >= 1 {
UserDefaults.standard.set(Date(), forKey: "lastUpdate")
yourLabel.text = quotes[randomInt(0,numberOfQuotes)]
}
} else { //firstTime running
UserDefaults.standard.set(Date(), forKey: "lastUpdate")
yourLabel.text = quotes[randomInt(0,numberOfQuotes)]
}
}
func randomInt(min: Int, max:Int) -> Int {
return min + Int(arc4random_uniform(UInt32((max - 1) - min + 1)))
}
根据您的描述,此代码按原样执行,完全符合您的要求.
This code is as is, by your description, it does exactly what you want.
这篇关于应用程序上的日常新内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文