@IBOutlet weak var ImageView: UIImageView!
@IBOutlet weak var Button: UIButton!
Imagenames = ["#imageLiteral(resourceName: "image2")", "#imageLiteral(resourceName: "image1"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func Buttonpress(_ sender: UIButton) {
let imageV = Int(arc4random_uniform(2))
ImageView.image = UIImage(named: Imagenames[ImageView])
}
如何解决此问题?请帮忙。运行最新版本的Xcode
答案 0 :(得分:2)
您在代码中忘记了一个简单的)
,而且还有一个额外的"
:
Imagenames = [#imageLiteral(resourceName: "image2"), #imageLiteral(resourceName: "image1")]
那应该可以挽救你的生命:)哈哈,有时会发生
答案 1 :(得分:2)
#imageLiteral
不应该进入String
。你的代码应该是
Imagenames = [#imageLiteral(resourceName: "image2"), #imageLiteral(resourceName: "image1")]
正如您已经看到的那样,代码在Xcode和Stackoverflow中都被正确突出显示。
请注意,您在var
前面缺少let
或Imagenames
。并且您的变量应以小写字母开头,例如images
。顺便说一句,你的功能也应遵循这个命名。
然后,您应该将Buttonpress
内的代码逻辑更改为:
ImageView.image = images[imageV]
将所有命名建议放在一起你的代码应该是这样的:
@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var button: UIButton!
let images = [#imageLiteral(resourceName: "image2"), #imageLiteral(resourceName: "image1")]
@IBAction func buttonPressed(_ sender: UIButton) {
let imageIndex = Int(arc4random_uniform(2))
imageView.image = images[imageIndex]
}