Skip to content

Instantly share code, notes, and snippets.

@popochess
Created March 28, 2018 16:51
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 popochess/64df4cb3a7996c66d13c975eba538f8d to your computer and use it in GitHub Desktop.
Save popochess/64df4cb3a7996c66d13c975eba538f8d to your computer and use it in GitHub Desktop.
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