-
-
Save popochess/64df4cb3a7996c66d13c975eba538f8d to your computer and use it in GitHub Desktop.
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
Shorthand Setter Declaration | |
If a computed property’s setter does not define a name for the new value to be set, a default name of newValue is used. Here’s an alternative version of the Rect structure, which takes advantage of this shorthand notation: | |
struct AlternativeRect { | |
var origin = Point() | |
var size = Size() | |
var center: Point { | |
get { | |
let centerX = origin.x + (size.width / 2) | |
let centerY = origin.y + (size.height / 2) | |
return Point(x: centerX, y: centerY) | |
} | |
set { | |
origin.x = newValue.x - (size.width / 2) | |
origin.y = newValue.y - (size.height / 2) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment