不能使用类型'()'的索引下标'[UIImage]'类型的值

时间:2017-03-18 02:37:28

标签: ios swift3 uiimage subscript xcplayground

我一直在收到这个错误:

  

不能下标'[UIImage]'类型的值,索引类型为'()'

这是我的代码:

let images: [UIImage] = [
    UIImage(named: "profilePicture.jpg")!,
    UIImage(named: "back3.jpg")!,
    UIImage(named: "back1.jpg")!]
var index = 0
let animationDuration: TimeInterval = 0.25
let switchingInterval: TimeInterval = 3

let chnagingImageView = UIImageView()

override func viewDidLoad() {

    chnagingImageView.frame = CGRect(x: 0, y: 300, width: view.frame.width, height: 200)
    view.addSubview(chnagingImageView)

    chnagingImageView.image = images[index += 1]
    animateImageView()

}

此行发生错误:

chnagingImageView.image = images[index += 1]

任何帮助将不胜感激。请注意我已经看到另一个问题,这只是游乐场的一个问题,它提供的解决方案是刷新代码。但是,这不起作用,我仍然收到错误。

2 个答案:

答案 0 :(得分:1)

 let images: [UIImage] = [
        UIImage(named: "profilePicture.jpg")!,
        UIImage(named: "back3.jpg")!,
        UIImage(named: "back1.jpg")!]
    var index = 0
    let animationDuration: TimeInterval = 0.25
    let switchingInterval: TimeInterval = 3

    let chnagingImageView = UIImageView()

    override func viewDidLoad() {

        chnagingImageView.frame = CGRect(x: 0, y: 300, width: view.frame.width, height: 200)
        view.addSubview(chnagingImageView)

    for myimage in images.count-1
     { 
      if myimage==1 
     { 
    chnagingImageView.image = images[myimage] 
     } 
    } 
       }

答案 1 :(得分:0)

将其更改为

 index += 1
 chnagingImageView.image = images[index]

因为index += 1返回() =>数组无法获取索引类型()

的值