gpt4 book ai didi

swift - 如何让标签在按下按钮时显示随机文本(不重复)?

转载 作者:行者123 更新时间:2023-11-30 10:01:56 25 4
gpt4 key购买 nike

我对 Swift 完全陌生,正在使用我的第一个显示随机文本的应用程序。文本不能重复,当显示所有文本时,应通知用户所有文本均已显示。

我收到了随机文本,但不是没有重复,并且不应该通知用户所有文本都已显示。

有人对我应该怎么做有建议吗?

我使用 Xcode 版本 7.3.1

let quotes: NSArray = ["Text0.", "Text1.", "Text2.", "Text3.", "Text4.", "Text5.", "Text6.", "Text7."]

let range: UInt32 = UInt32(quotes.count)

let randomNumber = Int(arc4random_uniform(range))
let QuoteString = quotes.objectAtIndex(randomNumber)

最佳答案

我是 GamePlayKit 中随机内容的忠实粉丝,因此我会使用这样的 GKRandomSource。

import GameplayKit

var str = "Hello, playground"

let quotes = ["Text0.", "Text1.", "Text2.", "Text3.", "Text4.", "Text5.", "Text6.", "Text7."]
let shuffledQuotes = GKRandomSource().arrayByShufflingObjectsInArray(quotes) as! [String]

for quote in shuffledQuotes {
print(quote)
}

print("All the text has been displayed")

这没有连接到按钮或任何东西,但我相信你能够解决这个问题......

关于swift - 如何让标签在按下按钮时显示随机文本(不重复)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38037743/

25 4 0