gpt4 book ai didi

ios - 随机选择一个UIImage

转载 作者:行者123 更新时间:2023-11-29 02:53:24 26 4
gpt4 key购买 nike

我需要从这些中选择一个。我将它们全部添加到 viewDidLoad 之前,我需要它来随机选择一个。我当前的代码返回错误 cannot assign to 'openingScreenDynamicDot in 'self'。这是如何解决的?

代码:

    let openingScreenDynamicDot = UIImage()
let dotOne = UIImage(named: "dot1.png")
let dotTwo = UIImage(named: "dot2.png")
let dotThree = UIImage(named: "dot3.png")
let dotFour = UIImage(named: "dot4.png")
let dotFive = UIImage(named: "dot5.png")
let dotSix = UIImage(named: "dot6.png")
let dotSeven = UIImage(named: "dot7.png")
let dotEight = UIImage(named: "dot8.png")
let dotNine = UIImage(named: "dot9.png")
let dotTen = UIImage(named: "dot10.png")
let dotEleven = UIImage(named: "dot11.png")
let dotTwelve = UIImage(named: "dot12.png")
let dotThirteen = UIImage(named: "dot13.png")
var imageNumber = arc4random()%13

override func viewDidLoad() {
let theRandomImages = [dotOne, dotTwo, dotThree, dotFour, dotFive, dotSix, dotSeven, dotEight, dotNine, dotTen, dotEleven, dotTwelve, dotThirteen]
openingScreenDynamicDot = theRandomImages.objectAtIndex(imageNumber)
}

最佳答案

您使用 let 关键字声明常量,使用 var 关键字声明变量。

所以把 let openingScreenDynamicDot 改成 var openingScreenDynamicDot

此外,swift 原生数组没有 objectAtIndex 方法,所以..

改变

openingScreenDynamicDot = theRandomImages.objectAtIndex(imageNumber)

openingScreenDynamicDot = theRandomImages[图像编号]

关于ios - 随机选择一个UIImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24214517/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com