Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use get and set in Swift

Tags:

ios

set

get

swift2

I have a block of simple code calculating the area & perimeter of a rhombus.

 var perimeterOfRhombus: Double {
        get {
            let twoSides1 = sideLength1 + sideLength1
            let twoSides2 = sideLength2 + sideLength2
            let finalPerimeter = twoSides1 + twoSides2
            return finalPerimeter
        } set {
      }
    }

What I am missing is the set section.

I want it to show what sideLength1 and sideLength2 were in the beginning.

I am not sure what newValue is used for, when it has no value.

It does work without using set, but I see on Apple's swift language guide that set and get usually go together.

If set is really not needed in this code, what is it normally used for anyways?

like image 284
Carol Smith Avatar asked Jul 19 '16 06:07

Carol Smith


People also ask

How does get and set work in Swift?

To create computed properties, Swift offers you a getter and (an optional) setter method to work with. A getter method is used to perform a computation when accessing the property. A setter method is an optional method. It can be used to modify a property that relates to the computed property.

What does get set mean in Swift?

The getting and setting of variables within classes refers to either retrieving ("getting") or altering ("setting") their contents. Consider a variable members of a class family . Naturally, this variable would need to be an integer, since a family can never consist of two point something people.

What is get property in Swift?

Working With Computed Properties in SwiftA getter method is used to perform a computation when requested. A setter method is an optional method. It can be used to modify a related property.

What is willSet and didSet in Swift?

willSet is called before the data is actually changed and it has a default constant newValue which shows the value that is going to be set. didSet is called right after the data is stored and it has a default constant oldValue which shows the previous value that is overwritten.


2 Answers

You should be referring to Read-Only Computed Properties instead of Computed Properties.

A computed property with a getter but no setter is known as a read-only computed property.It always returns a value, and cannot be set to a different value.

 var perimeterOfRhombus: Double {
  let twoSides1 = sideLength1 + sideLength1
  let twoSides2 = sideLength2 + sideLength2
  let finalPerimeter = twoSides1 + twoSides2
  return finalPerimeter
 }

Computed Properties do not actually store a value. Instead, they provide a getter and an optional setter to retrieve and set other properties and values indirectly.

Computed properties, including read-only properties must be set as a variable and not a constant.

 struct Square {
  var edge: Double = 0

  var area: Double {
    get {
      return edge * edge
    }
    set (newArea){
      edge = sqrt(newArea)
    }
  }
 }

In this case, setter can't be used to set the value of the property but to set the values of other properties, from which the computed property is computed.

About newValue, it's called Shorthand Setter Declaration - If a computed property’s setter does not define a name for the new value to be set, a default name of newValue is used.

So, the above example becomes

 struct Square {
  var edge: Double = 0

  var area: Double {
    get {
      return edge * edge
    }
    set {
      edge = sqrt(newValue) //someSquare.area = 25 (25 is newValue)
    }
  }
 }
like image 199
Ajith R Nayak Avatar answered Oct 18 '22 03:10

Ajith R Nayak


Computed property in swift using get and set

class Square {
    var side: Double = 0.0
    var area: Double {
        get {
            return side*side
        }
        set(newArea) {
            print(newArea)
            self.side = sqrt(newArea)
        }
    }
}

let obj = Square()
obj.side = 10.0
print(obj.area)
like image 1
2 revs, 2 users 80% Avatar answered Oct 18 '22 05:10

2 revs, 2 users 80%