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