繁体   English   中英

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

[英]Set value to computed properties in SWIFT

我试图在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)")
        }

编辑:但是我发现我可以更改其派生的计算属性的其他属性

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

但是,如果我想改变计算属性iteself,该怎么办?

计算属性就是:计算值,在您的情况下从宽度和高度。 没有存储属性值的实例变量,您不能更改“计算属性本身”。

这没有任何意义:如果区域可以设置为不同的值,那么getter方法应该返回什么? 这个新值或width*height

所以很可能你想要一个区域的只读计算属性:

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

正如您已经注意到的,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
        }
    }
}

但即便如此,结果可能会令人惊讶(由于整数舍入):

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

我认为你误解了计算属性的概念。 根据定义,计算属性是您无法设置其值的属性,因为它是计算出来的。 它没有独立存在。 计算属性中setter的目的不是设置属性的值,而是设置计算计算属性的其他属性的值。 以广场为例。 它的边长是一个var属性s,该区域是一个计算属性,其getter返回s * s,其setter设置为newValue(新区域)的平方根。 设定者不设定区域。 它设置下次访问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