簡體   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