Initializers의 확장
struct Size {
var width = 0.0, height = 0.0
}
struct Point {
var x = 0.0, y = 0.0
}
struct Rect {
var origin = Point()
var size = Size()
}
let defaultRect = Rect()
let memberwiseRect = Rect(origin: Point(x: 2.0, y: 2.0),
size: Size(width: 5.0, height: 5.0))
extension Rect {
init(center: Point, size: Size) {
let originX = center.x - (size.width / 2)
let originY = center.y - (size.height / 2)
self.init(origin: Point(x: originX, y: originY), size: size)
}
}
let centerRect = Rect(center: Point(x: 4.0, y: 4.0),
size: Size(width: 3.0, height: 3.0))
// centerRect's origin is (2.5, 2.5) and its size is (3.0, 3.0)
'Smart Device > Swift' 카테고리의 다른 글
Swift extension #4 Mutating Instance Methods (0) | 2015.06.25 |
---|---|
Swift extension #3 Method의 확장 (0) | 2015.06.25 |
Swift extension #1 기존 자료형의 확장 (0) | 2015.06.25 |
Swift Deinitialization (0) | 2015.06.24 |
Swift Initializer Delegation for Value Types (0) | 2015.06.24 |