Created
June 4, 2014 21:38
-
-
Save RyanCopley/698aabd5ffa2c7366cb1 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
“struct Point { | |
var x = 0.0, y = 0.0 | |
} | |
struct Size { | |
var width = 0.0, height = 0.0 | |
} | |
struct Rect { | |
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(newCenter) { | |
origin.x = newCenter.x - (size.width / 2) | |
origin.y = newCenter.y - (size.height / 2) | |
} | |
} | |
} | |
var square = Rect(origin: Point(x: 0.0, y: 0.0), | |
size: Size(width: 10.0, height: 10.0)) | |
let initialSquareCenter = square.center | |
square.center = Point(x: 15.0, y: 15.0) | |
println("square.origin is now at (\(square.origin.x), \(square.origin.y))") | |
// prints "square.origin is now at (10.0, 10.0)” | |
Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks. https://itun.es/us/jEUH0.l |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment