1

I am trying to assign a property a new value inside a struct but when I try to I get "Cannot assign 'x' in 'self' ".

struct gallery {

var x = 0


let picture = [
    "landscape.jpg",
    "paris.jpg",
    "polygons.jpg"
]

func getImage() -> String{
    if (x > picture.count){
        x = 0
        return picture[x]
    } else{
        return picture[x]
    }
}
} 
  • Not regarding your question, but your `x > picture.count` will fail if your x is equal to the `picture.count`, use `x >= picture.count` – milo526 Apr 18 '15 at 19:30

1 Answers1

3

If a method changes your instance, you have to indicate it with the mutating keyword like so:

mutating func getImage() -> String {

        if (x > picture.count) {

            x = 0
            return picture[x]

        } else {
            return picture[x]
        }
    }
József Vesza
  • 4,775
  • 3
  • 27
  • 42