简体   繁体   中英

Set value to computed properties in SWIFT

i am trying to learn computed properties in swift..and knew that i need setter to set value for computed properties..i am trying to but stuck..please help me how do i set value in area with setter properties...and would be great if you could tell me how to use setter property and when to use it

 class ViewController: UIViewController {
        var width : Int = 20
        var height : Int = 400
        var area: Int{
            get{
                return width * height
            }set(newarea){
                area = newarea*10
          //these line gives me an warning and area is not set
            }
        }

        override func viewDidLoad() {
            super.viewDidLoad()
            println("\(width)")
            println("\(height)")
            println("\(area)") 
         //  gives an error while setting value to computed properties...       area = 5000
         //  for that we need getter and setter properties in area...
            area = 490
            println("the new area for computed properties is as \(area)")
        }

EDIT: however i figured out i can change the other properties of computed properties from which it is derived as

set(newValue){
           // self.area = newValue
            width = newValue/10
            println("the new width for computed properties is as \(width)")
        }
    }

But what if i want to change the computed property iteself

A computed property is just that: A computed value, in your case from width and height. There is no instance variable where the properties value is stored, you cannot change the "computed property itself".

And it makes no sense: If the area could be set to a different value, what should the getter method return? This new value or width*height ?

So most probably you want a read-only computed property for the area:

var area: Int {
    get {
        return width * height
    }
}

As you already noticed, the setter can modify the values of other stored properties, for example:

class Rectangle {
    var width : Int = 20
    var height : Int = 400

    var area: Int {
        get {
            return width * height
        }
        set(newArea){
            // Make it a square with the approximate given area:
            width = Int(sqrt(Double(newArea)))
            height = width
        }
    }
}

But even then the results may be surprising (due to integer rounding):

let r = Rectangle()
r.area = 200
println(r.area) // 196

I think you're misunderstanding the concept of a computed property. By definition, a computed property is one whose value you cannot set because, well, it's computed. It has no independent existence. The purpose of the setter in a computed property is not to set the value of the property but to set the values of other properties, from which the computed property is computed. Take, as an example, a square. Its side length is a var property s and the area is a computed property whose getter returns s*s and whose setter sets s to be the square root of newValue (the new area). The setter does not set the area. It sets the side length from which the area will get computed the next time you access the area property.

Rather than storing a value ,Computed property provides a getter and ,optionally a setter which indirectly retrieve and set other properties and values respectively .

struct Point 
 {
var x = 0.0 ,y = 0.0
}
struct Shape 
{
var origin = Point ( )
var centre : Point {
get
{
return Point (x:origin.x/2 y:origin.y/2)
}
set(newCentre )
{
origin.x = newCentre.x/2
origin.y = newCentre.y/2
}
}
}
}

The Shape structure defines a custom getter and setter method for computed variable called Centre .The Centre 
property then access through dot syntax ,which causes getter for centre to be called retrieve the current property value .Rather than returning a existing value ,the getter actually calculates and returns a new point that represent centre of the shape .
     If a Computed property setter does not define a name for the  new value to be set a default name of "newValue" is used 

   Below is an alternative version of Rect structure ,which takes advantage of this shorthand notation.

struct Point 
 {
var x = 0.0 ,y = 0.0
}
struct Shape 
{
var origin = Point ( )
var centre : Point {
get
{
return Point (x:origin.x/2 y:origin.y/2)
}
set(newCentre )
{
origin.x = newValue.x/2
origin.y = newValue.y/2
}
}
}
}

Important - A computed property with a getter but no setter is known as read only Computed Property .It always returns a value and can be accessed through dot syntax .However the value can't be altered .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM