【发布时间】:2017-07-17 17:23:03
【问题描述】:
在我的 iOS 应用程序中,我有一些类似于今日报价的内容。我将如何每天更新“报价”。我在变量中有引号,例如
var quotes = ["The most amazing quote in the world", "This makes no sense"]
我想把它发到viewDidLoad。非常感谢您的帮助!
【问题讨论】:
在我的 iOS 应用程序中,我有一些类似于今日报价的内容。我将如何每天更新“报价”。我在变量中有引号,例如
var quotes = ["The most amazing quote in the world", "This makes no sense"]
我想把它发到viewDidLoad。非常感谢您的帮助!
【问题讨论】:
有很多方法可以做到这一点,例如,如果我们为每个月的每一天做一个报价,例如,只要确保有 31 个报价,您就可以使用当月的日期来索引您的报价数组:
let date = Date()
let calendar = Calendar.current
let dayOfMonth = calendar.component(.day, from: date)
quoteOfDay = quotes[dayOfMonth] // Make sure you have 31 quotes
更新:如果您想要更多随机性,您可以使用hashValue 然后:
let date = Date()
let dateIndex = Int(date.timeIntervalSince1970) / (60*60*24)
let quoteOfDay = quotes[dateIndex % quotes.count]
【讨论】:
Int(arc4random_uniform(UInt32(quotes.count))) 随机化怎么样?我该怎么做?
Int(date.timeIntervalSince1970) % 50 作为您的索引(包含 50 个引号)
Int(date.timeIntervalSince1970) % 50 可以固定并每天更改,并假设总共有 50 个报价。 (更新答案以获得更多随机但固定的日期基于hashValue)