-1

I have been recieving this error:

cannot subscript a value of type '[UIImage]' with an index of type '()'

Here is my code:

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()

}

The error occurs in this line:

chnagingImageView.image = images[index += 1]

Any help would be appreciated. Please note I have seen another question that this is simply a problem with playgrounds and the solution it provides is to just refresh the code. However, this does not work and I still receive the error.

Arasuvel
  • 2,971
  • 1
  • 25
  • 40
Rohan Vasishth
  • 241
  • 1
  • 6
  • 14

2 Answers2

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] 
     } 
    } 
       }
Tushar Sharma
  • 2,839
  • 1
  • 16
  • 38
0

Change it to

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

because index += 1 return () => arrays cannot get value with index type ()

Luan Tran
  • 1,142
  • 7
  • 15