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

我正在试图学习计算的属性在swift ..知道我需要二传手设置值的计算属性..我试图但卡住..请帮助我如何设置值与设置属性的区域…和如果你能告诉我如何使用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

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

这是没有意义的:如果该区域可以设置为一个不同的值,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属性,面积是一个计算属性,它的getter返回s * s,其setter设置s为newValue(新区域)的平方根。 制定者不设置区域。 它会设置下次访问区域属性时计算区域的边长。

 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 .