将值设置为SWIFT中的计算属性 [英] Set value to computed properties in SWIFT

查看:93
本文介绍了将值设置为SWIFT中的计算属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习swift中的计算属性..并且知道我需要setter来设置计算属性的值。我正试图但卡住..请帮助我如何设置区域中的值与setter属性。 ..如果你能告诉我如何使用setter属性以及何时使用它会很棒

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)")
        }

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

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)")
        }
    }

但如果我想更改计算属性iteself

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".

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

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

这篇关于将值设置为SWIFT中的计算属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆