【发布时间】:2020-01-07 19:19:05
【问题描述】:
我一直在用 swift 开发一个附带项目,该项目基本上是当日流行词应用程序。我已经将 30 个单词和内容存储到一个数组中,并且每天都在尝试更新单词。我尝试了一些修复但无济于事。即使我更改手机上的日期,我也只能显示第一个数组。
wordNumber 和 wordOfDay 是否冲突?我怎样才能适应这种情况,以便每天都会弹出一个新词?
let wordList =
[
Words(word: "aaa", pronounciation: "bbbb", type: "noun", definition:"blah"),
Words(word: "bbb", pronounciation: "cccc", type: "adjective", definition:"blah")
]
var wordNumber = 0;
class ViewController: UIViewController {
@IBOutlet weak var wordLabel: UILabel!
@IBOutlet weak var pronounciationLabel: UILabel!
@IBOutlet weak var wordTypeLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
updateWord()
}
@IBAction func prepareForUnwind (segue: UIStoryboardSegue){}
func updateWord()
{
let date = Date()
let dateIndex = Int(date.timeIntervalSince1970) / (60*60*24)
let wordOfDay = wordList[dateIndex % wordList.count]
wordLabel.text = wordList[wordNumber].word
pronounciationLabel.text = wordList[wordNumber].pronounciation
}
}
【问题讨论】:
-
您希望每天出现随机单词。对吗?
-
你用正确的数字填充 wordOfDay,然后使用保持为 0 的 wordNumber ...
-
“即使我更改手机上的日期,我也只能显示第一个数组” - 第一个数组是什么意思?
-
抱歉,我知道这很令人困惑。我只得到数组中的 0 位置。