Shuyamaru
Shuyamaru

Reputation: 65

Why need get/set when using Computed Properties in Swift, while we can code without them actually?

Here's an example from Official Guide by Apple:

struct​ ​Point​ {
​    ​var​ ​x​ = ​0.0​, ​y​ = ​0.0
​}
​struct​ ​Size​ {
​    ​var​ ​width​ = ​0.0​, ​height​ = ​0.0
​}
​struct​ ​Rect​ {
​    ​var​ ​origin​ = ​Point​()
​    ​var​ ​size​ = ​Size​()
​    ​var​ ​center​: ​Point​ {
​        ​get​ {
​            ​let​ ​centerX​ = ​origin​.​x​ + (​size​.​width​ / ​2​)
​            ​let​ ​centerY​ = ​origin​.​y​ + (​size​.​height​ / ​2​)
​            ​return​ ​Point​(​x​: ​centerX​, ​y​: ​centerY​)
​        }
​        ​set​(​newCenter​) {
​            ​origin​.​x​ = ​newCenter​.​x​ - (​size​.​width​ / ​2​)
​            ​origin​.​y​ = ​newCenter​.​y​ - (​size​.​height​ / ​2​)
​        }
​    }
​}
​var​ ​square​ = ​Rect​(​origin​: ​Point​(​x​: ​0.0​, ​y​: ​0.0​),
​    ​size​: ​Size​(​width​: ​10.0​, ​height​: ​10.0​))
​let​ ​initialSquareCenter​ = ​square​.​center
​square​.​center​ = ​Point​(​x​: ​15.0​, ​y​: ​15.0​)
​println​(​"square.origin is now at (​\(​square​.​origin​.​x​)​, ​\(​square​.​origin​.​y​)​)"​)
​// prints "square.origin is now at (10.0, 10.0)”

This make me pretty confused why we need get/set here, even we actually can do without them, then I tried:

struct anotherRect {
    var origin = Point()
    var size = Size()
    var center: Point{
        return Point(x: (origin.x + size.width/2), y: (origin.y + size.height/2))
    }
}

var anotherSquare = Rect(origin: Point(x: 20.0, y: 20.0), size: Size(width: 10.0, height: 10.0))
println("anotherSquare.center is (\(anotherSquare.center.x), \(anotherSquare.center.y))")
// prints "anotherSquare.center is (25.0, 25.0)"

anotherSquare.center = Point(x: 30.0, y: 30.0)
println("anotherSquare.origin is now at (\(anotherSquare.origin.x), \(anotherSquare.origin.y))")
// prints "anotherSquare.origin is now at (25.0, 25.0)"

With this, I can do get value or set a new value to the computed properties, exactly the same get/set by Apple's way. Do we really need get/set in our code? and Why?

I'm just a newcomer, so feel free let me know all about this stuff? I really appriciate your help!!

Upvotes: 0

Views: 69

Answers (1)

Ricardo Sanchez-Saez
Ricardo Sanchez-Saez

Reputation: 9566

They are not the same. If you don't specify the get set keywords, you only implement the getter.

struct AnotherRect {
    var origin = Point()
    var size = Size()
    var center: Point {
        return Point(x: (origin.x + size.width/2), y: (origin.y + size.height/2))
    }
}

is equivalent to

struct AnotherRect {
    var origin = Point()
    var size = Size()
    var center: Point {
        get {
            return Point(x: (origin.x + size.width/2), y: (origin.y + size.height/2))
        }
    }
}

Your example got you confused because you have an error in your code:

var anotherSquare = Rect(origin: Point(x: 20.0, y: 20.0), size: Size(width: 10.0, height: 10.0))

should be

var anotherSquare = AnotherRect(origin: Point(x: 20.0, y: 20.0), size: Size(width: 10.0, height: 10.0))

Try that and you will notice that you cannot assign to the center property.


Also, the convention is that you name your custom types starting with a capital letter, e.g.: AnotherRect.

Upvotes: 2

Related Questions