[Swift๐Ÿฆฉ] #13 ์ƒ์†

8274 ๋‹จ์–ด swiftiOSiOS
  • ํด๋ž˜์Šค ๋Š” ๋ฉ”์†Œ๋“œ, ํ”„๋กœํผํ‹ฐ, ํŠน์ง• ์„ ๋‹ค๋ฅธ ํด๋ž˜์Šค์—์„œ ์ƒ์†ํ•ด์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ๊ตฌ์กฐ์ฒด์™€ ํด๋ž˜์Šค์˜ ๊ฐ€์žฅ ํฐ ์ฐจ์ด์ .
  • ์ €์žฅ ํ”„๋กœํผํ‹ฐ์™€ ๊ณ„์‚ฐ ํ”„๋กœํผํ‹ฐ์— ๊ด€๊ณ„์—†์ด, ์ƒ์†๋ฐ›์€ ํ”„๋กœํผํ‹ฐ์— ํ”„๋กœํผํ‹ฐ ์˜ต์ €๋ฒ„๋ฅผ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

1. Base Class ์ •์˜

  • ๋‹ค๋ฅธ ์–ด๋–ค ํด๋ž˜์Šค๋„ ์ƒ์†๋ฐ›์ง€ ์•Š์€ ํด๋ž˜์Šค.

์˜ˆ์‹œ) currentSpeed ์™€ makeNoise ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” Vehicle class

class Vehicle {
    var currentSpeed = 0.0
    var description: String {
        return "traveling at \(currentSpeed) miles per hour"
    }
    func makeNoise() {
        // do nothing - an arbitrary vehicle doesn't necessarily make a noise
    }
}

print("Vehicle: \(someVehicle.description)")
// Vehicle: traveling at 0.0 miles per hour

2. Subclassing == ์ƒ์†

  • ๋ถ€๋ชจ๋กœ๋ถ€ํ„ฐ ์„ฑ๊ฒฉ์„ ์ƒ์†๋ฐ›๊ณ , ์ž์‹ ์˜ ๊ณ ์œ  ํŠน์„ฑ์„ ์ถ”๊ฐ€ํ•  ์ˆ˜๋„ ์žˆ๋‹ค.
// Vehicle ์˜ ํ”„๋กœํผํ‹ฐ, ๋ฉ”์„œ๋“œ, ํŠน์ง•์„ ์‚ฌ์šฉํ•˜๋ฉด์„œ,
// Bicycle ๋งŒ์˜ ํ”„๋กœํผํ‹ฐ์ธ hasBasket ๋„ ์ถ”๊ฐ€.
class Bicycle: Vehicle {
    var hasBasket = false
}

bicycle.currentSpeed = 15.0
print("Bicycle: \(bicycle.description)")
// Bicycle: traveling at 15.0 miles per hour


// ํ•œ๋ฒˆ subclassing ๋œ ๊ฒƒ์„ ๋‹ค์‹œ subclassing ๊ฐ€๋Šฅ.
class Tandem: Bicycle {
    var currentNumberOfPassengers = 0
}

3. Overriding โญ๏ธ

  • ์„œ๋ธŒํด๋ž˜์Šค์—์„œ ๋ถ€๋ชจ ํด๋ž˜์Šค์—์„œ ์ƒ์†๋ฐ›์€ ๊ฒƒ์„ ์žฌ์ •์˜ํ•˜๋Š” ๊ฒƒ.
  • ์ธ์Šคํ„ด์Šค/ํƒ€์ž… ํ”„๋กœํผํ‹ฐ, ๋ฉ”์„œ๋“œ, ์„œ๋ธŒ์Šคํฌ๋ฆฝํŠธ์— ๋ชจ๋‘ ๊ฐ€๋Šฅํ•˜๋‹ค.
  • override ํ‚ค์›Œ๋“œ๋ฅผ ๋ถ™์—ฌ์„œ ๋ถ€๋ชจ์˜ ๊ฒƒ์„ ์žฌ์ •์˜ ํ•˜๋Š” ๊ฒƒ์ž„์„ ์•Œ๋ฆฐ๋‹ค.

4. ๋ถ€๋ชจ ํด๋ž˜์Šค์˜ ๊ฒƒ์— ์ ‘๊ทผ.

  • ์ž์‹์—์„œ ๋ถ€๋ชจ์˜ ๋ฉ”์†Œ๋“œ, ํ”„๋กœํผํ‹ฐ, ์„œ๋ธŒ์Šคํฌ๋ฆฝํŠธ์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค.
  • super ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
  • super.someMethod, super.someProperty, super[someIndex]

overriding method

class Train: Vehicle {
    override func makeNoise() {
        print("Choo Choo")
    }
}

let train = Train()
train.makeNoise()
// Prints "Choo Choo" : ์ƒˆ๋กœ ์ •์˜ํ•œ ๋‚ด์šฉ์ด ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค.

properties

  • ์ €์žฅ ํ”„๋กœํผํ‹ฐ, ๊ณ„์‚ฐ ํ”„๋กœํผํ‹ฐ ๋ชจ๋‘ ์˜ค๋ฒ„๋ผ์ด๋“œ๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค.
  • ํƒ€์ž…, ์ด๋ฆ„์„ ๋ชจ๋‘ ๋ช…์‹œํ•ด์•ผ ํ•œ๋‹ค.
  • ๋ถ€๋ชจ์˜ read-only property ๋ฅผ read-write property ๋กœ ์žฌ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค. (getter/setter ๋ชจ๋‘ ์ •์˜ํ•ด์„œ)
  • ๋ฐ˜๋Œ€๋กœ ๋ถ€๋ชจ์˜ read-write property ๋ฅผ read-only ๋กœ ์žฌ์ •์˜ ํ•˜๋Š” ๊ฒƒ์€ ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค.
  • setter ๋ฅผ override ํ• ๊ฑฐ๋ฉด getter ๋„ ๋ฐ˜๋“œ์‹œ ์ œ๊ณตํ•ด์•ผ ํ•จ.
class Car: Vehicle {
    var gear = 1
    override var description: String {
        return super.description + " in gear \(gear)"
    }
}

Property Observers

  • ๋ถ€๋ชจ ํด๋ž˜์Šค์— ์„ ์–ธ๋œ ํ”„๋กœํผํ‹ฐ ์˜ต์ €๋ฒ„๋ฅผ ์žฌ์ •์˜ ํ•˜๋Š” ๊ฒƒ๋„ ๊ฐ€๋Šฅ.
  • ํ•˜์ง€๋งŒ, let ์ด๋‚˜ read-only ์—๋Š” ํ”„๋กœํผํ‹ฐ ์˜ต์ €๋ฒ„๋ฅผ ๋ถ™์ผ ์ˆ˜ ์—†๋‹ค.
    set ํ•  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ
  • ๊ฐ™์€ ํ”„๋กœํผํ‹ฐ์— observer ์™€ setter ๋ฅผ ๋™์‹œ์— ์ถ”๊ฐ€ํ•ด์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๋„ ํ•  ์ˆ˜ ์—†๋‹ค.
    setter ๋Š” observer์™€ ๊ฐ™์€ ๋™์ž‘์„ ํ•˜๊ธฐ ๋•Œ๋ฌธ
class AutomaticCar: Car {
    override var currentSpeed: Double {
        didSet {
            gear = Int(currentSpeed / 10.0) + 1
        }
    }
}

์˜ค๋ฒ„๋ผ์ด๋“œ ๋ฐฉ์ง€

  • ์ž์‹ ํด๋ž˜์Šค์—์„œ ํŠน์ • ๋ฉ”์†Œ๋“œ, ํ”„๋กœํผํ‹ฐ, ์„œ๋ธŒ์Šคํฌ๋ฆฝํŠธ๊ฐ€ ์˜ค๋ฒ„๋ผ์ด๋“œ ๋˜์ง€ ์•Š๊ฒŒ ํ•˜๋ ค๋ฉด
  • final ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์˜ค๋ฒ„๋ผ์ด๋“œ๋ฅผ ๋ฐฉ์ง€ํ•œ๋‹ค.

-> BaseVC ๊ฐ™์€ ๊ฒƒ ๋ง๊ณ , ๋งˆ์ง€๋ง‰์œผ๋กœ ์„ ์–ธํ•˜๋Š” VC์—๋Š” final ์„ ์จ์„œ ์ตœ์ ํ™”๋ฅผ ํ•ด์ฃผ์ž.

์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ