Search code examples
iosswift3uiimagesubscriptxcplayground

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


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.


Solution

  • Change it to

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

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