简体   繁体   English

将值设置为SWIFT中的计算属性

[英]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 我试图在swift中学习计算属性..并且知道我需要setter来设置计算属性的值。我正试图但卡住了..请帮助我如何在setter属性中设置值...并且如果你能告诉我如何使用setter属性以及何时使用它会很棒

 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 但是,如果我想改变计算属性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? 这没有任何意义:如果区域可以设置为不同的值,那么getter方法应该返回什么? This new value or width*height ? 这个新值或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: 正如您已经注意到的,setter可以修改其他存储属性的值,例如:

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. 计算属性中setter的目的不是设置属性的值,而是设置计算计算属性的其他属性的值。 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). 它的边长是一个var属性s,该区域是一个计算属性,其getter返回s * s,其setter设置为newValue(新区域)的平方根。 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. 它设置下次访问area属性时计算区域的边长。

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 .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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