๐Ÿฆ… Swift

Properties

Kiwi๐Ÿ’ป 2022. 7. 4. 16:00

ํด๋ž˜์Šค๋‚˜ ๊ตฌ์กฐ์ฒด, ์—ด๊ฑฐ์ฒด์˜ ๊ฐ์ฒด ์ธ์Šคํ„ด์Šค๊ฐ€ ๊ทธ ๋‚ด๋ถ€์— ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๊ฐ์ฒด์˜ ์ƒํƒœ์— ๊ด€ํ•œ ์ •๋ณด๋ฅผ ๋งํ•œ๋‹ค.

  • ์ €์žฅ ํ”„๋กœํผํ‹ฐ: ์ธ์Šคํ„ด์Šค์˜ ์ผ๋ถ€๋กœ ์ƒ์ˆ˜์™€ ๋ณ€์ˆ˜ ๊ฐ’์„ ์ €์žฅ
  • ๊ณ„์‚ฐ ํ”„๋กœํผํ‹ฐ: ๊ฐ’์„ ์ €์žฅํ•˜๋Š” ๋Œ€์‹ ์— ๊ณ„์‚ฐ์„ ํ•จ( ํด๋ž˜์Šค, ๊ตฌ์กฐ์ฒด, ์—ด๊ฑฐํ˜•์—์„œ ์ œ๊ณต)
  • ํƒ€์ž… ํ”„๋กœํผํ‹ฐ: ํ”„๋กœํผํ‹ฐ๋Š” ํƒ€์ž… ๊ทธ ์ž์ฒด์™€ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ์„ ์ˆ˜ ์žˆ๋Š”๋ฐ, ์ด๋ฅผ ํƒ€์ž…ํ”„๋กœํผํ‹ฐ๋ผ๊ณ  ํ•จ

์ €์žฅ ํ”„๋กœํผํ‹ฐ (Stored Properties)

์ €์žฅ ํ”„๋กœํผํ‹ฐ๋Š” ํŠน์ • ํด๋ž˜์Šค ๋˜๋Š” ๊ตฌ์กฐ์ฒด์˜ ์ธ์Šคํ„ด์Šค ์ผ๋ถ€๋กœ์„œ ์ €์žฅ๋œ ์ƒ์ˆ˜ ๋˜๋Š” ๋ณ€์ˆ˜์ด๋‹ค. ๋˜ํ•œ ์ €์žฅ ํ”„๋กœํผํ‹ฐ๋ฅผ ์„ ์–ธํ• ๋•Œ “๊ธฐ๋ณธ๊ฐ’ (Default Property Value)”๋ฅผ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด๋Ÿฌํ•œ ์ €์žฅ ํ”„๋กœํผํ‹ฐ๋Š” initializer(์ƒ์„ฑ์ž)๋กœ ์ดˆ๊ธฐํ™” ์‹œํ‚ฌ ์ˆ˜ ๋„ ์žˆ๋‹ค. ์ƒ์„ฑ์ž์— ์˜ํ•ด ์ดˆ๊ธฐํ™” ๋  ๋•Œ๋Š” ํ•ด๋‹น ํ”„๋กœํผํ‹ฐ๊ฐ€ ์ƒ์ˆ˜๋กœ ์„ ์–ธ ๋˜์—ˆ๋‹ค๊ณ  ํ•ด๋„ ์ˆ˜์ •์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

struct FixedLengthRange {
    var firstValue: Int
    let length: Int
}
var rangeOfThreeItems = FixedLengthRange(firstValue: 0, length: 3)
// the range represents integer values 0, 1, and 2
rangeOfThreeItems.firstValue = 6
// the range now represents integer values 6, 7, and 8

์ƒ์ˆ˜๋กœ ์ •์˜๋œ ๊ตฌ์กฐ์ฒด ์ธ์Šคํ„ด์Šค (Stored Properties of Constant Structure Instances)

๊ตฌ์กฐ์ฒด์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์ƒ์ˆ˜์— ํ• ๋‹นํ•˜๋ฉด ํ”„๋กœํผํ‹ฐ๊ฐ€ ๋ณ€์ˆ˜๋กœ ์„ ์–ธ๋˜์–ด ์žˆ์–ด๋„ ์ธ์Šคํ„ด์Šค์˜ ํ”„๋กœํผํ‹ฐ๋ฅผ ์ˆ˜์ •ํ•  ์ˆ˜ ์—†๋‹ค.

let rangeOfFourItems = FixedLengthRange(firstValue : 0, length: 4)
// ์ด ๋ฒ”์œ„๋Š” ์ •์ˆ˜ ๊ฐ’ 0, 1, 2, 3 ์„ ๋‚˜ํƒ€๋ƒ„

rangeOfFourItems.firstValue = 6. 
// ์ด๋Š” firstValue ๊ฐ€ ๋ณ€์ˆ˜ ์†์„ฑ์ผ์ง€๋ผ๋„, ์—๋Ÿฌ๋ฅผ ๋ฐ˜ํ™˜

์ด๋Ÿฌํ•œ ๋™์ž‘์€ ๊ตฌ์กฐ์ฒด๊ฐ€ ๊ฐ’ ํƒ€์ž… ์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๊ฐ’ ํƒ€์ž…์˜ ์ธ์Šคํ„ด์Šค๊ฐ€ ์ƒ์ˆ˜๋กœ ์„ ์–ธํ•˜๋ฉด ์ธ์Šคํ„ด์Šค์˜ ๋ชจ๋“  ํ”„๋กœํผํ‹ฐ์— ๋Œ€ํ•ด ์ˆ˜์ •ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

ํ•˜์ง€๋งŒ, ์ฐธ์กฐํƒ€์ž…์ธ ํด๋ž˜์Šค์—์„œ๋Š” ์ด๊ฒƒ๊ณผ ๋‹ค๋ฅธ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜ํƒ€๋‚œ๋‹ค. ํด๋ž˜์Šค ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์ˆ˜๋กœ ์„ ์–ธํ•ด๋„ ์„ ์–ธํ•œ ํ”„๋กœํผํ‹ฐ์˜ ๊ฐ’์€ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค.

์ง€์—ฐ ์ €์žฅ ํ”„๋กœํผํ‹ฐ (Lazy Stored Properties)

์ง€์—ฐ ์ €์žฅ ํ”„๋กœํผํ‹ฐ (lazy stored property)๋Š” ์ฒ˜์Œ ํ˜ธ์ถœ๋  ๋•Œ๊นŒ์ง€ ์ดˆ๊ธฐ๊ฐ’์€ ๊ณ„์‚ฐ๋˜์ง€ ์•Š๋Š” ํ”„๋กœํผํ‹ฐ ์ด๋‹ค. ์ง€์—ฐ ์ €์žฅ ํ”„๋กœํผํ‹ฐ๋Š” ์„ ์–ธ ์ „์— lazy ํ‚ค์›Œ๋“œ๋ฅผ ๋ถ™์—ฌ ๋‚˜ํƒ€๋‚ธ๋‹ค.

NOTEโ—๏ธ
์ธ์Šคํ„ด์Šค ์ดˆ๊ธฐํ™”๊ฐ€ ์™„๋ฃŒ๋œ ํ›„์—๋„ ์ดˆ๊ธฐ๊ฐ’์ด ์—†์„ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ์ง€์—ฐ ํ”„๋กœํผํ‹ฐ๋Š” var ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ณ€์ˆ˜๋กœ ์„ ์–ธํ•ด์•ผ ํ•œ๋‹ค. ํ”„๋กœํผํ‹ฐ ์ƒ์ˆ˜๋Š” ์ดˆ๊ธฐํ™”๊ฐ€ ์™„๋ฃŒ๋˜๊ธฐ ์ „์— ํ•ญ์ƒ ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์žˆ์–ด์•ผ ํ•˜๋ฏ€๋กœ lazy๋กœ ์„ ์–ธํ•  ์ˆ˜ ์—†๋‹ค.

ํ•ด๋‹น ์†์„ฑ์ด ๋ฐ˜๋“œ์‹œ ์ฒ˜์Œ๋ถ€ํ„ฐ ์ดˆ๊ธฐํ™”๊ฐ€ ํ•„์š”ํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ(์ผ๋ฐ˜์ ์œผ๋กœ ๋งŽ์€ ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„์„ ์ฐจ์ง€ํ•˜๋Š” ์ด๋ฏธ์ง€ ๋“ฑ)์— ์ดˆ๊ธฐํ™”๋ฅผ ์ง€์—ฐ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค.
(๋ถˆํ•„์š”ํ•œ ์„ฑ๋Šฅ์ €ํ•˜๋‚˜, ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„์˜ ๋‚ญ๋น„ ์ค„์ผ ์ˆ˜ ์žˆ์Œ)

๊ณ„์‚ฐ ํ”„๋กœํผํ‹ฐ (Computed Properties)

ํด๋ž˜์Šค, ๊ตฌ์กฐ์ฒด, ์—ด๊ฑฐํ˜•์€ ๊ณ„์‚ฐ๋œ ํ”„๋กœํผํ‹ฐ๋ฅผ ์„ ์–ธํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด ์—ฐ์‚ฐํ”„๋กœํผํ‹ฐ๋Š” ์‹ค์ œ ๊ฐ’์„ ์ €์žฅํ•˜๊ณ  ์žˆ๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, getter๊ณผ optionalํ•œ setter๋ฅผ ์ œ๊ณตํ•ด ๊ฐ’์„ ํƒ์ƒ‰ํ•˜๊ณ  ๊ฐ„์ ‘์ ์œผ๋กœ ๋‹ค๋ฅธ ํ”„๋กœํผํ‹ฐ ๊ฐ’์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์„ ์ œ๊ณตํ•œ๋‹ค. setter๋Š” ํ•„์ˆ˜์ ์ธ ์š”์†Œ๋Š” ์•„๋‹ˆ๊ณ  ์„ ํƒ์‚ฌํ•ญ์ด๋‹ค.

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
// initialSquareCenter is at (5.0, 5.0)
square.center = Point(x: 15.0, y: 15.0)
print("square.origin is now at (\(square.origin.x), \(square.origin.y))")
// Prints "square.origin is now at (10.0, 10.0)"

์œ„ ์ฝ”๋“œ๋Š” ์ขŒํ‘œ์™€ํฌ๊ธฐ๋ฅผ ๊ฐ–๋Š” ์‚ฌ๊ฐํ˜•์„ ํ‘œํ˜„ํ•˜๋Š” ๊ตฌ์กฐ์ฒด์— ๊ด€ํ•œ ์ฝ”๋“œ์ด๋‹ค. ์—ฌ๊ธฐ์„œ Rect ๊ตฌ์กฐ์ฒด๋Š” ์‚ฌ๊ฐํ˜•์˜ ์ค‘์ ์„ ํ‘œํ˜„ํ•˜๋Š” center๋ผ๋Š” ์—ฐ์‚ฐ ํ”„๋กœํผํ‹ฐ๋ฅผ ์ œ๊ณตํ•œ๋‹ค. ์ด ํ”„๋กœํผํ‹ฐ๋Š” ๊ณ„์‚ฐ๋œ ํ”„๋กœํผํ‹ฐ์˜ ์ •์˜๋Œ€๋กœ ๊ฐ’์„ ์ง์ ‘ ๊ฐ–๊ณ  ์žˆ๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๋‹ค๋ฅธ ์ขŒํ‘œ์™€ ํฌ๊ธฐ ํ”„๋กœํผํ‹ฐ๋“ค์„ ์ ์ ˆํžˆ ์—ฐ์‚ฐํ•ด์„œ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค. (get) ๋˜ set์œผ๋กœ ์‚ฌ๊ฐํ˜•์˜ ์ค‘์ ์„ ์ง์ ‘ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, ์ด ๊ฐ’์„ ์„ค์ •ํ•  ๋•Œ x, y ์ขŒํ‘œ๊ฐ€ ์–ด๋–ค ๊ฐ’์„ ๊ฐ€์ ธ์•ผํ•˜๋Š”์ง€ x, y์— ์ ์ ˆํ•œ ์ขŒํ‘œ๊ฐ’์„ ๋„ฃ์–ด์ค€๋‹ค.

square.center ์„ ํ†ตํ•ด square ๋ณ€์ˆ˜์˜ center ํ”„๋กœํผํ‹ฐ์— ์ ‘๊ทผํ•˜๋Š”๋ฐ, ์ด๋Š” center์˜ getter์„ ํ˜ธ์ถœํ•˜์—ฌ ํ˜„์žฌ ์†์„ฑ๊ฐ’์„ ๊ฐ€์ ธ์˜จ๋‹ค. ๊ธฐ์กด ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค๊ธฐ ๋ณด๋‹ค๋Š”, ์ƒˆ Point๋ฅผ ๊ณ„์‚ฐํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•ด์ค€๋‹ค. ์œ„์ฝ”๋“œ์—์„œ ๋ณด๋ฉด, getter๋Š” (5,5)๋ผ๋Š” ์ค‘์‹ฌ์ ์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

๊ทธ ๋‹ค์Œ, center ํ”„๋กœํผํ‹ฐ์— (15, 15)๋ผ๋Š” ์ƒˆ๋กœ์šด ๊ฐ’์„ ์„ค์ •ํ–ˆ๋‹ค. ์ด๋Š” ์ •์‚ฌ๊ฐํ˜•์„ ์˜ค๋ฅธ์ชฝ ์œ„, ์•„๋ž˜ ๊ทธ๋ฆผ์—์„œ๋Š” ์˜ค๋ Œ์ง€์ƒ‰ ์ •์‚ฌ๊ฐํ˜•์˜ ์œ„์น˜๋กœ ์ด๋™์‹œํ‚จ๋‹ค. center ํ”„๋กœํผํ‹ฐ๋ฅผ ์„ค์ •ํ•˜๋ฉด center์˜ setter์„ ํ˜ธ์ถœํ•˜๋Š”๋ฐ ์ด๋Š” orgin์ด๋ผ๋Š” ์ €์žฅ ํ”„๋กœํผํ‹ฐ์˜ x์™€ y๊ฐ’์„ ์ˆ˜์ •ํ•˜์—ฌ ์ •์‚ฌ๊ฐํ˜•์„ ์ƒˆ ์œ„์น˜๋กœ ์ด๋™์‹œํ‚จ๋‹ค.

square ๋ผ๋Š” ์ƒˆ๋กœ์šด Rect ๋ณ€์ˆ˜๋ฅผ ์ƒ์„ฑํ–ˆ๋‹ค. square๋ณ€์ˆ˜๋Š” (0,0) ์ด๋ผ๋Š” ์›์ ๊ณผ, (10, 10)์ด๋ผ๋Š” ํญ๊ณผ ๋†’์ด๋กœ ์ดˆ๊ธฐํ™”๋œ๋‹ค. ์•„๋ž˜ ๊ทธ๋ฆผ์˜ ํŒŒ๋ž€์ƒ‰ ์‚ฌ๊ฐํ˜•์ด ์ด ์‚ฌ๊ฐํ˜•์ด๋‹ค.

Shorted Setter Declaration (Setter ์„ ์–ธ์˜ ๊ฐ„๋žตํ•œ ํ‘œํ˜„)

์•ž์˜ ์ฝ”๋“œ์—์„œ๋Š” Setter์˜ ์ธ์ž ์ด๋ฆ„์„ ์•„๋ž˜์™€ ๊ฐ™์ด set(newCenter)๋ผ๊ณ  ๋ช…์‹œํ–ˆ์ง€๋งŒ, ๋งŒ์•ฝ ์ด๋ ‡๊ฒŒ newCenter๋ผ๊ณ  ์ธ์ž ์ด๋ฆ„์„ ์ •ํ•˜์ง€ ์•Š์œผ๋ฉด ์ธ์ž ๊ธฐ๋ณธ ์ด๋ฆ„์ธ newValue๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

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

์œ„ ์ฝ”๋“œ์—์„œ๋Š” set ๋ฉ”์†Œ๋“œ ์•ˆ์—์„œ ์ธ์ž ์ด๋ฆ„์„ ์ง€์ •ํ•˜์ง€ ์•Š์•˜๋Š”๋ฐ๋„ newValue.x, newValue.y๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์„ ๋ณด์‹ค ์ˆ˜ ์žˆ๋‹ค.

Shorthand Getter Declaration

์ „์ฒด getter ๋ณธ๋ฌธ์ด ๋‹จ์ผ ํ‘œํ˜„์‹์ด๋ฉด, getter์€ ์•”์‹œ์ ์œผ๋กœ ๊ทธ ํ‘œํ˜„์‹์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

struct CompactRect {
    var origin = Point()
    var size = Size()
    var center: Point {
        get {
            Point(x: origin.x + (size.width / 2),
                  y: origin.y + (size.height / 2))
        }
        set {
            origin.x = newValue.x - (size.width / 2)
            origin.y = newValue.y - (size.height / 2)
        }
    }
}

getter์—์„œ return์„ ์ƒ๋žตํ–ˆ๋‹ค. ์ด๋Š” ํ•จ์ˆ˜์—์„œ return์„ ์ƒ๋žตํ•˜๋Š” ๊ฒƒ๊ณผ ๋™์ผํ•œ ๊ทœ์น™์„ ๊ฐ–๋Š”๋‹ค.

์ฝ๊ธฐ์ „์šฉ ๊ณ„์‚ฐ๋œ ํ”„๋กœํผํ‹ฐ (Read-Only Computed Properties)

getter๋งŒ ์žˆ๊ณ  setter๋ฅผ ์ œ๊ณตํ•˜์ง€ ์•Š๋Š” ๊ณ„์‚ฐ๋œ ํ”„๋กœํผํ‹ฐ๋ฅผ ์ฝ๊ธฐ์ „์šฉ ๊ณ„์‚ฐ๋œ ํ”„๋กœํผํ‹ฐ๋ผ๊ณ  ํ•œ๋‹ค. ์ฆ‰, ์ฝ๊ธฐ์ „์šฉ ๊ณ„์‚ฐ๋œ ํ”„๋กœํผํ‹ฐ๋Š” ๋ฐ˜๋“œ์‹œ ๋ฐ˜ํ™˜ ๊ฐ’์„ ์ œ๊ณตํ•˜๊ณ  ๋‹ค๋ฅธ ๊ฐ’์„ ์ง€์ •ํ•  ์ˆ˜๋Š” ์—†๋Š” ํ”„๋กœํผํ‹ฐ์ด๋‹ค.

NOTEโ—๏ธ
์ฝ๊ธฐ์ „์šฉ ๊ณ„์‚ฐ๋œ ํ”„๋กœํผํ‹ฐ๋ฅผ ํฌํ•จํ•ด ๊ณ„์‚ฐ๋œ ํ”„๋กœํผํ‹ฐ๋ฅผ ์„ ์–ธ์‹œ์—๋Š” ๋ฐ˜๋“œ์‹œ let์ด ์•„๋‹ˆ๋ผ var๋กœ ์„ ์–ธํ•ด์•ผํ•œ๋‹ค. ๋ณดํ†ต ์ฝ๊ธฐ์ „์šฉ(read-only)์ด๋ผ ํ•จ์€ ํ•œ๋ฒˆ ๊ฐ’์ด ์ •ํ•ด์ง€๋ฉด ๋ณ€ํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— let์œผ๋กœ ์„ ์–ธํ•˜๋Š” ๊ฒƒ์ด ๋งž์œผ๋‚˜ ๊ณ„์‚ฐ๋œ ํ”„๋กœํผํ‹ฐ๋Š” ์ฝ๊ธฐ์ „์šฉ(read-only)์ด๋ผ ํ•˜๋”๋ผ๋„ ๊ณ„์‚ฐ ๊ฐ’์— ๋”ฐ๋ผ ๊ฐ’์ด ๋ณ€ํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— var๋กœ ์„ ์–ธํ•œ๋‹ค.

ํ”„๋กœํผํ‹ฐ ๊ด€์ฐฐ์ž(Property Observers)

์†์„ฑ ๊ด€์ฐฐ์ž๋Š” ์†์„ฑ ๊ฐ’์ด ๋ฐ”๋€Œ๋Š” ๊ฑธ ๊ด€์ฐฐํ•˜์—ฌ ์‘๋‹ตํ•ฉ๋‹ˆ๋‹ค. ์†์„ฑ ๊ด€์ฐฐ์ž๋Š” ์†์„ฑ ๊ฐ’์„ ์„ค์ •ํ•  ๋•Œ๋งˆ๋‹ค(์ƒˆ ๊ฐ’์ด ํ˜„์žฌ ์†์„ฑ ๊ฐ’๊ณผ ๊ฐ™์•„๋„) ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค.

  • willSet : ๊ฐ’์ด ์ €์žฅ๋˜๊ธฐ ๋ฐ”๋กœ ์ง์ „์— ํ˜ธ์ถœ ๋จ
  • didSet : ์ƒˆ ๊ฐ’์ด ์ €์žฅ๋˜๊ณ  ๋‚œ ์งํ›„์— ํ˜ธ์ถœ ๋จ

willSet์—์„œ๋Š” ์ƒˆ ๊ฐ’์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋ช…์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, ์ง€์ •ํ•˜์ง€ ์•Š์œผ๋ฉด ๊ธฐ๋ณธ ๊ฐ’์œผ๋กœ newValue๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
didSet์—์„œ๋Š” ๋ฐ”๋€Œ๊ธฐ ์ „์˜ ๊ฐ’์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋ช…์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, ์ง€์ •ํ•˜์ง€ ์•Š์œผ๋ฉด ๊ธฐ๋ณธ ๊ฐ’์œผ๋กœ oldValue๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

class StepCounter {
    var totalSteps: Int = 0 {
        willSet(newTotalSteps) {
            print("About to set totalSteps to \(newTotalSteps)")
        }
        didSet {
            if totalSteps > oldValue  {
                print("Added \(totalSteps - oldValue) steps")
            }
        }
    }
}
let stepCounter = StepCounter()
stepCounter.totalSteps = 200
// About to set totalSteps to 200
// Added 200 steps
stepCounter.totalSteps = 360
// About to set totalSteps to 360
// Added 160 steps
stepCounter.totalSteps = 896
// About to set totalSteps to 896
// Added 536 steps

'๐Ÿฆ… Swift' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

Subscripts  (0) 2022.07.04
Methods  (0) 2022.07.04
Structures and Classes  (0) 2022.06.22
Enumerations  (0) 2022.06.15
Closures  (0) 2022.06.15