【发布时间】:2016-10-03 02:46:13
【问题描述】:
我在一个结构中使用GKRandomSource 在视图中返回一个随机的励志名言。有没有办法返回该随机数并省略先前的条目?这样用户就不会连续两次收到相同的报价。
let inspiration = [
"You are looking rather nice today, as always.",
"Hello gorgeous!",
"You rock, don't ever change!",
"Your hair is looking on fleek today!",
"That smile.",
"Somebody woke up on the right side of bed!"]
func getRandomInspiration() -> String {
let randomNumber = GKRandomSource.sharedRandom().nextIntWithUpperBound(inspiration.count)
return inspiration[randomNumber]
}
【问题讨论】:
-
最好有一个数组的副本,每次取随机索引时,将其从数组中删除,然后从 0 随机到新数组大小
标签: ios swift random shuffle gameplay-kit