Rohan Vasishth
Rohan Vasishth

Reputation: 241

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.

Upvotes: -1

Views: 799

Answers (2)

Tushar Sharma
Tushar Sharma

Reputation: 2882

 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] 
     } 
    } 
       }

Upvotes: 1

Luan Tran
Luan Tran

Reputation: 1142

Change it to

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

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

Upvotes: 0

Related Questions