1

I was trying to get the grasp of getters and setters in Swift. Here's some code that I wrote.

class SomeClass {
 var var1: Int
 var var2: Int{
    get{
        return 7 * var1
    }
    set(newValue){
        newValue * var1
    }
  }
    init(_ var1: Int){
        self.var1 = var1
    }
    
}

var obj = SomeClass(3)
var objComputedProp = obj.var2
print(objComputedProp) //this prints 21

objComputedProp = 14 //I want to change var2's value to 14 (give it a newValue),
                     //so that var2(which is objComputedProp) returns 14*var1 (which would be 42)

print(objComputedProp)//this prints 14

I understand that when we write var obj = SomeClass(3), var1 in the class becomes 3 and the getter returns 7 * var1. But how do I access the setter? Setting objComputedProp to 14 just assigns it 14.

I also tried changing var1, but this just prints the same number twice:

var obj = SomeClass(3)
var objComputedProp = obj.var2
print(objComputedProp)

obj = SomeClass(5)
print(objComputedProp)
anon
  • 73
  • 9
  • 1
    `objComputedProp` is an `Int`, which is a value-type. It doesn't change just because you update `obj`, and it won't change unless you assign another value to it directly. That's irrespective of your setter. But also, you're not actually setting the value to anything in the setter. The idea is that you can set the value inside the setter to other internal properties and hide the implementation from the user of the property. Your implementation doesn't update anything, so setting `obj.var2` doesn't change anything. – New Dev Mar 20 '21 at 08:52
  • 1
    objComputedProp is a Int variable, it has nothing to do with the object `obj` that you have created. If you want to change obj.var2 then work with the object. `obj.var2 = 14`. I would suggest you first learn about objects (instances) , properties and variables so you understand the difference – Joakim Danielson Mar 20 '21 at 08:52
  • 1
    In this snippet, `objComputedProp` is just a variable that holds the value it gets from `obj.var2`, it isn't `var2`, which means that assigning a value to `objComputedProp` you're setting the value of the variable itself, you have nothing to do with the `obj.var2` property here, you have to directly assign that 14 to `obj.var2` =) I guess – Dorin Baba Mar 20 '21 at 08:55
  • See https://stackoverflow.com/questions/24025340/property-getters-and-setters#24025548 – Shadowrun Mar 20 '21 at 09:08

1 Answers1

3

Assigning obj.var2 to objComputedProp makes a copy of the value due to value semantics.

The line

objComputedProp = 14 

doesn't call the setter of var2.

Apart from that it makes no sense to set the computed variable itself in the setter because it causes an infinite loop.

The setter is supposed to modify the associated variable used in the getter for example

set(newValue){
     var1 = var1 * newValue
}

Another example of a computed variable is for Core Data. The computed property maps a non-supported to a supported type, an array of String to a comma separated single String and vice versa

@NSManaged var string : String

var list : [String] {
    get { return string.components(separatedBy: ",") }
    set { string = newValue.joined(separator: ",") }
}
vadian
  • 274,689
  • 30
  • 353
  • 361