Skip to content

Instantly share code, notes, and snippets.

@aainaj
Created April 22, 2019 00:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aainaj/054d3fccfad763617ed5b3252d28116d to your computer and use it in GitHub Desktop.
Save aainaj/054d3fccfad763617ed5b3252d28116d to your computer and use it in GitHub Desktop.
Computed property example
import Foundation
struct Rectangle {
var width = 0.0
var height = 0.0
var area: Double {
set {
width = sqrt(newValue)
height = sqrt(newValue)
}
get {
return width * height
}
}
}
var rect = Rectangle()
print(rect.area)
rect.area = 10
print(rect.height)
print(rect.area)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment