๐Ÿ“ฑ App Development Study/iOS๐ŸŽ

[Udemy iOS & Swift Bootcamp] Advanced Swift Properties (Computed/Observed Properties)

ibelieveinme 2023. 7. 25. 23:32
728x90

1. Computed Properties

: ๊ฐ’์ด ๊ณ„์† ๊ฐฑ์‹ ๋˜๋Š” ์†์„ฑ

 

Pizza Inch ์— ๋”ฐ๋ผ Slice ์˜ ๊ฐœ์ˆ˜๋ฅผ ์•Œ์•„๋ณด์ž.

Pizza Inch ๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ Inch - 4 ๋ฅผ ํ•˜๋ฉด Slice ๊ฐœ์ˆ˜๋ฅผ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

import Foundation

let pizzaInInches: Int = 10
var nunberOfSlices: Int = 6

print(pizzaInInches)

์ด๋ฅผ ์ง์ ‘ ๋ณ€์ˆ˜์•ˆ์— ๊ฐ’์„ ์ง€์ •ํ•ด์„œ ๋„ฃ๋Š” ๋ฐฉ๋ฒ•์€ ์œ„์™€ ๊ฐ™๋‹ค.

์ด์™ธ์— Slice ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด์„œ ๋ฐ˜ํ™˜ํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

 

import Foundation

let pizzaInInches: Int = 10
var nunberOfSlices: Int {
    return pizzaInInches - 4
}

print(pizzaInInches)

 

์œ„์™€ ๊ฐ™์ด Computed Properties ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ถ”๊ฐ€ ๋ฉ”์„œ๋“œ๋ฅผ ๋งŒ๋“ค์ง€ ์•Š๊ณ  Computed ๋ณ€์ˆ˜ ์„ ์–ธ๋งŒ์œผ๋กœ ๊ฐ’์„ ์ž๋™์œผ๋กœ ๋ณ€ํ•˜๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๊ธฐ์— getter ๋ณ€์ˆ˜์ด๋‹ค.

 

import Foundation

let pizzaInInches: Int = 10
var nunberOfSlices: Int {
    get{
        return pizzaInInches - 4
    }
}

print(pizzaInInches)

์ด๋ ‡๊ฒŒ get ์œผ๋กœ ๊ฐ์‹ธ๋Š”๊ฑฐ๋ž‘ ๋˜‘๊ฐ™๋‹ค. ๋งŽ์ด ๋ดค๋˜ ๊ตฌ์กฐ!

 

* Couputed Properties ๊ตฌํ˜„์‹œ ์ฃผ์˜์‚ฌํ•ญ

1) var ๋กœ ์„ ์–ธํ•  ๊ฒƒ.

2) ๋ฐ์ดํ„ฐ ํƒ€์ž…์„ ๋ช…์‹œ์ ์œผ๋กœ ์ง€์ •ํ•ด์ค˜์•ผ ํ•œ๋‹ค. ํ˜•์‹ ์ถ”๋ก  ๋ถˆ๊ฐ€.

 

Computed Properties ์˜ getter ๋ฅผ ์•Œ์•„๋ดค์œผ๋‹ˆ setter ๋ฅผ ์•Œ์•„๋ณด์ž.

import Foundation

let pizzaInInches: Int = 10
var numberOfPeople: Int = 12
let slicesPerPerson: Int = 3

var numberOfSlices: Int {
    get{
        return pizzaInInches - 4
    }
}

var numberOfPizza: Int{
    get {
        let numberOfPeopleFedPerPizza = numberOfSlices / slicesPerPerson
        return numberOfPeople / numberOfPeopleFedPerPizza
    }
    set {
        let totalSlices = numberOfSlices * newValue
        numberOfPeople = totalSlices / slicesPerPerson
    }
}

numberOfPizza = 4

print(pizzaInInches)

newValue ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๊ฐ’์— set ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

์•„๋ž˜ 2๋ฒˆ์งธ์ค„ numberOfPizza = 4 ๋กœ set ๊ฐ’์„ ์ฃผ๋ฉด ์ˆ˜์‹์— ๋”ฐ๋ผ totalSlices, numberOfPeople ๊ฐ’์ด ์ž๋™์œผ๋กœ ๊ฐฑ์‹ ๋œ๋‹ค.

numberOfPeople ๊ฐ’์„ ์ถœ๋ ฅํ–ˆ์„ ๋•Œ, ๋งจ ์ฒ˜์Œ ์ €์žฅํ•œ 12 ๊ฐ’์ด ์•„๋‹Œ 8 ๊ฐ’์ด ์ถœ๋ ฅ๋œ๋‹ค.


P.S) ์ž๋™ ์‹คํ–‰ ์†๋„๊ฐ€ ๋Š๋ ค์„œ File Inspector ๋กœ ์ด๋™ํ•ด์„œ  Platform ์„ macOS ๋กœ ๋ฐ”๊พธ๊ณ  UIKit ๋Œ€์‹ ์— Foundation ์„ import ํ•ด์คฌ๋‹ค.

import Foundation

 

2. Observed Properties

: Property ๊ฐ’์˜ ๋ณ€๊ฒฝ์‚ฌํ•ญ์„ ๋ชจ๋‹ˆํ„ฐ๋ง ํ•  ์ˆ˜ ์žˆ๋Š” ์†์„ฑ

1) willSet - ๊ฐ’์ด ์ €์žฅ๋˜๊ธฐ ์ง์ „์— ํ˜ธ์ถœ.

2) didSet - ์ƒˆ๋กœ์šด ๊ฐ’์ด ์ €์žฅ๋œ ์งํ›„์— ํ˜ธ์ถœ.

 

import Foundation

var pizzaInInches: Int = 10 {
    willSet{
        
    }
    didSet{
        if pizzaInInches >= 18 {
            print("Invalid size specified, pizzaInInches set to 18.")
            pizzaInInches = 18
        }
    }
}

pizzaInInches = 20
print(pizzaInInches)
Invalid size specified, pizzaInInches set to 18.
18

๋งˆ์ง€๋ง‰ ๋ฐ”๋กœ ์œ—์ค„์—์„œ pizzaInInches ์˜ ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๋ฉด didSet ๊ตฌ๋ฌธ์ด ํ˜ธ์ถœ๋œ๋‹ค.

 

width, height, ํŽ˜์ธํŠธ ์–‘์ด ์ฃผ์–ด์งˆ ๋•Œ ํŽ˜์ธํŠธ๊ฐ€ ๋ช‡ํ†ต์ด ํ•„์š”ํ•œ์ง€๋ฅผ ์•Œ์•„๋ณด์ž.

import Foundation

var width: Float = 1.5
var height: Float = 2.3

var bucketsOfPaint: Int {
    get {
        let area = width * height
        let areaCoveredPerBucket: Float = 1.5
        let numberOfBuckets = area / areaCoveredPerBucket
        let roundedBuckets = ceilf(numberOfBuckets)
        return Int(numberOfBuckets)
    }
    set {
        let areaCanCover = Float(newValue) * 1.5
        print("This amount of paint can cover an area of \(areaCanCover)")
    }
}


bucketsOfPaint = 4
This amount of paint can cover an area of 6.0

print ๊ตฌ๋ฌธ์œผ๋กœ bucketsOfPaint ๊ฐ’์„ ์•Œ์•„์˜ค๋ผ๊ณ  ํ–ˆ์„ ๋•Œ, get ๊ตฌ๋ฌธ์ด ์‹คํ–‰๋œ๋‹ค.

์ด ๋•Œ, ceilf ํ•จ์ˆ˜๋Š” ๋ฌด์กฐ๊ฑด ๋ฐ˜์˜ฌ๋ฆผ ํ•˜๋Š” ํ•จ์ˆ˜์ด๋‹ค.

 

 

728x90