我在结构中使用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 :(得分:2)
要避免生成相同的引号,请跟踪名为struct
的{{1}}属性中的最后一个。然后将最大随机数减少1,如果生成与lastQuote
相同,请改用lastQuote
。
max