Created
April 22, 2019 00:11
-
-
Save aainaj/054d3fccfad763617ed5b3252d28116d to your computer and use it in GitHub Desktop.
Computed property example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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