Property getters and setters

With this simple class I am getting the compiler warning

Attempting to modify/access x within its own setter/getter

and when I use it like this:

var p: point = Point()
p.x = 12

I get an EXC_BAD_ACCESS. How can I do this without explicit backing ivars?

class Point {

    var x: Int {
        set {
            x = newValue * 2 //Error
        }
        get {
            return x / 2 //Error
        }
    }
    // ...
}

12 Answers
12

Leave a Comment