预期','分隔符添加图像时

时间:2017-06-05 18:37:46

标签: ios swift

@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

2 个答案:

答案 0 :(得分:2)

您在代码中忘记了一个简单的),而且还有一个额外的"

Imagenames = [#imageLiteral(resourceName: "image2"), #imageLiteral(resourceName: "image1")]

那应该可以挽救你的生命:)哈哈,有时会发生

答案 1 :(得分:2)

#imageLiteral不应该进入String。你的代码应该是

Imagenames = [#imageLiteral(resourceName: "image2"), #imageLiteral(resourceName: "image1")]

正如您已经看到的那样,代码在Xcode和Stackoverflow中都被正确突出显示。

请注意,您在var前面缺少letImagenames。并且您的变量应以小写字母开头,例如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]
}