我有以下数组:
var notebookCovers = ["cover1", "cover2", "cover3", "cover4", "cover4", "cover6", "cover7", "cover8", "cover9", "cover10"]
Run Code Online (Sandbox Code Playgroud)
并且UIButton当它被按下时,它会生成一个UIImage带有数组元素的新元素.
我需要做的是每次按下按钮以从数组中生成随机但唯一的元素(不重复元素),直到它们全部被选中,然后再次重新启动数组.
到目前为止,我有一个随机元素,但它重复,我无法弄清楚如何它,所以它每次都获得一个独特的图像
func createNewNotebook() {
let newNotebook = Notebook()
let randomInt = randomNumber()
newNotebook.coverImageString = notebookCovers[randomInt]
notebooks.insert(newNotebook, at: 0)
collectionView.reloadData()
}
func randomNumber() -> Int {
var previousNumber = arc4random_uniform(UInt32(notebookCovers.count))
var randomNumber = arc4random_uniform(UInt32(notebookCovers.count - 1))
notebookCovers.shuffle()
if randomNumber == previousNumber {
randomNumber = UInt32(notebookCovers.count - 1)
}
previousNumber = randomNumber
return Int(randomNumber)
}
Run Code Online (Sandbox Code Playgroud)