2019년 1월 8일 화요일

Coumputed Properties


  
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)
print("square.origin is now at (\(square.origin.x), \(square.origin.y))")


// Shorthand Setter Declaration

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)
       }
   }
}

// Read-Only Coputed Properties

struct Cuboid {
   var width = 0.0, height = 0.0, depth = 0.0
   var volume: Double {
       return width * height * depth
   }
}
let fourByFiveByTwo = Cuboid(width: 4.0, height: 5.0, depth: 2.0)
print("the volue of fourByFiveByTwo is \(fourByFiveByTwo.volume)")