Skip to content

Latest commit

ย 

History

History
350 lines (281 loc) ยท 11.6 KB

initialization.md

File metadata and controls

350 lines (281 loc) ยท 11.6 KB

Setting Initial Values for Stored Properties

  • ํด๋ž˜์Šค์™€ ์ŠคํŠธ๋Ÿญ์ฒ˜๋Š” ์ธ์Šคํ„ด์Šค๊ฐ€ ์ƒ์„ฑ๋˜๋Š” ์‹œ์ ์— ๋ชจ๋“  stored property๊ฐ€ ์ดˆ๊ธฐํ™”๋˜์–ด์•ผ ํ•œ๋‹ค.
  • ์˜ต์…”๋„ ํ”„๋กœํผํ‹ฐ๋˜ํ•œ nil๋กœ ์ดˆ๊ธฐํ™” ๋˜๋Š” ๊ฒƒ์ด๋‹ค.
  • ๊ธฐ๋ณธ๊ฐ’์„ ํ• ๋‹นํ•˜๋Š” 2๊ฐ€์ง€ ๋ฐฉ๋ฒ•(์ •์˜์™€ ๋™์‹œ์— ํ• ๋‹น, ์ƒ์„ฑ์ž์—์„œ ํ• ๋‹น) ๋‘˜๋‹ค ํ”„๋กœํผํ‹ฐ ์˜ต์ €๋ฒ„๋ฅผ ํ˜ธ์ถœํ•˜์ง€ ์•Š๋Š”๋‹ค.
  • ์•„๋ž˜ ์ฝ”๋“œ์—์„œ willSet๊ณผ didSet์€ a.height = 40์— ํ•œ๋ฒˆ๋งŒ ํ˜ธ์ถœ๋จ
struct Person {
    var height = 170 { //์—ฌ๊ธฐ์„œ 170์ด ํ• ๋‹น๋˜์ง€๋งŒ ํ”„๋กœํผํ‹ฐ ์˜ต์ €๋ฒ„ ํ˜ธ์ถœ X
        didSet {
            print(oldValue)
        }
        willSet {
            print(newValue)
        }
    }
    
    init() {
        self.height = 30 //์—ฌ๊ธฐ์„œ 30์ด ํ• ๋‹น๋˜์ง€๋งŒ ํ”„๋กœํผํ‹ฐ ์˜ต์ €๋ฒ„ ํ˜ธ์ถœ X
    }
    
    
}

var a = Person()
a.height = 40
  • ๊ฒฐ๊ณผ
40
30
Program ended with exit code: 0

Default Initializers

  • ๊ตฌ์กฐ์ฒด๋‚˜ ํด๋ž˜์Šค์—์„œ ๋ชจ๋“  ํ”„๋กœํผํ‹ฐ๊ฐ€ ๊ธฐ๋ณธ๊ฐ’์„ ๊ฐ–๊ณ  ์žˆ๊ฑฐ๋‚˜ ์˜ต์…”๋„์ด๊ณ  ์ƒ์„ฑ์ž๋ฅผ ํ•˜๋‚˜๋„ ๊ตฌํ˜„ํ•˜์ง€ ์•Š์•˜๋‹ค๋ฉด Default Initializer๊ฐ€ ์‹คํ–‰๋œ๋‹ค.

  • Default Initializer = ๋ชจ๋“  ํ”„๋กœํผํ‹ฐ์˜ ๊ฐ’์„ ๊ธฐ๋ณธ๊ฐ’(default value)๋กœ ํ• ๋‹นํ•ด์ฃผ๊ณ  ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜๋Š” ์ƒ์„ฑ์ž

  • ์˜ˆ์‹œ, init()์„ ๊ตฌํ˜„ํ•˜์ง€ ์•Š์•˜์ง€๋งŒ ๊ธฐ๋ณธ๊ฐ’์„ ํ• ๋‹นํ•ด์ฃผ๋Š” Default ์ƒ์„ฑ์ž๊ฐ€ ํ˜ธ์ถœ๋จ

class ShoppingListItem {
    var name: String?
    var quantity = 1
    var purchased = false
}
var item = ShoppingListItem()

Memberwise Initializers for Structure Types (ํด๋ž˜์Šค์—๋Š” ์—†์Œ)

  • ๊ตฌ์กฐ์ฒด์—์„œ ๊ธฐ๋ณธ์ ์œผ๋กœ ์ œ๊ณตํ•˜๋Š” ์ƒ์„ฑ์ž
  • ์ด๋ฏธ ๊ธฐ๋ณธ๊ฐ’์ด ํ• ๋‹น๋œ ๋ณ€์ˆ˜๋Š” ์ƒ๋žตํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ์˜ˆ์‹œ
struct Size {
    var width = 0.0, height = 0.0
}

//width, height ๋‘˜๋‹ค ํ• ๋‹น
let twoByTwo = Size(width: 2.0, height: 2.0)

//width ์ƒ๋žต
let zeroByTwo = Size(height: 2.0)

//height ์ƒ๋žต
let zeroByZero = Size()

Initializer Delegation for Value Types

  • Initializer Delegation = ์ƒ์„ฑ์ž ์•ˆ์—์„œ ๋‹ค๋ฅธ ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ์ฝ”๋“œ

  • Initializer Delegation์€ Value type๊ณผ class type ๋‘˜๋‹ค ์žˆ์Œ

  • Value type์€ ์ƒ์†์ด ์—†๊ธฐ๋•Œ๋ฌธ์— ๊ทœ์น™์ด ๋” ๋‹จ์ˆœํ•จ

  • Initializer Delegation์ด ์ผ์–ด๋‚˜๋Š” ์ฝ”๋“œ์—์„œ๋„ Convenience ํ‚ค์›Œ๋“œ๊ฐ€ ํ•„์š”์—†์Œ

  • Custom ์ƒ์„ฑ์ž๋ฅผ value type์— ์ •์˜ํ•˜๋ฉด, ๋” ์ด์ƒ ๊ธฐ๋ณธ ์ƒ์„ฑ์ž๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Œ

    • ๊ตฌ์กฐ์ฒด๋ผ๋ฉด ๋ฉค๋ฒ„์™€์ด์ฆˆ ์ƒ์„ฑ์ž ๋˜ํ•œ ์ ‘๊ทผ ๋ชปํ•จ
    • Custom ์ƒ์„ฑ์ž์™€ ๊ธฐ๋ณธ ์ƒ์„ฑ์ž๋ฅผ ๋‘˜๋‹ค ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด Custom ์ƒ์„ฑ์ž๋ฅผ extension์— ์ •์˜ํ•˜๋ฉด ๋จ
  • ์˜ˆ์‹œ

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()
    init() {}
    init(origin: Point, size: Size) {
        self.origin = origin
        self.size = size
    }
    //์ด๋ถ€๋ถ„์—์„œ delegation์ด ์ผ์–ด๋‚จ. ํ•˜์ง€๋งŒ convenience ํ‚ค์›Œ๋“œ๊ฐ€ ํ•„์š”์—†์Œ
    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)
    }
}

Class Inheritance and Initialization

  • ํด๋ž˜์Šค์—๋งŒ convenience init๊ณผ designated init์ด ์žˆ์Œ
  • ๋ชจ๋“  ํด๋ž˜์Šค์—๋Š” ์ตœ์†Œ ํ•œ๊ฐœ์˜ designated ์ƒ์„ฑ์ž๊ฐ€ ์žˆ์–ด์•ผํ•จ
  • ๊ฐ™์€ ํด๋ž˜์Šค์˜ ๋‹ค๋ฅธ ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•˜๋ ค๋ฉด convenience init์œผ๋กœ ๋งŒ๋“ค์–ด์•ผ ํ•œ๋‹ค.

Initializer Delegation for Class Types

Rule 1 A designated initializer must call a designated initializer from its immediate superclass. Rule 2 A convenience initializer must call another initializer from the same class. Rule 3 A convenience initializer must ultimately call a designated initializer.

A simple way to remember this is:

  • Designated initializers must always delegate up.
  • Convenience initializers must always delegate across.
  1. ๊ฐ™์€ ํด๋ž˜์Šค์˜ ๋‹ค๋ฅธ ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•˜๋ ค๋ฉด convenience ํ‚ค์›Œ๋“œ๊ฐ€ ํ•„์š”ํ•จ
  2. ์ƒ์œ„ ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž(super.init())์€ designated ์ƒ์„ฑ์ž์—์„œ๋งŒ ๊ฐ€๋Šฅํ•˜๊ณ  ์—ฌ๊ธฐ์„œ ๋ถˆ๋ ค์ง€๋Š” ์ƒ์„ฑ์ž ๋˜ํ•œ designated ์ƒ์„ฑ์ž๋งŒ ๊ฐ€๋Šฅํ•จ

Two-Phase Initialization

  • ํด๋ž˜์Šค์˜ ์ดˆ๊ธฐํ™”๋Š” 2๋‹จ๊ณ„๋กœ ์ด๋ฃจ์–ด์ง

1 phase๋Š” ์œ„๋กœ ์˜ฌ๋ผ๊ฐ€๋Š” ๊ณผ์ • 2 phase๋Š” ์•„๋ž˜๋กœ ๋‚ด๋ ค๊ฐ€๋Š” ๊ณผ์ •

  • ํ•ด๋‹น ๊ฐ์ฒด์— ๋ฉ”์†Œ๋“œ์—๋Š” 2phase์—์„œ๋งŒ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ์ƒ์œ„ ํด๋ž˜์Šค์˜ ํ”„๋กœํผํ‹ฐ๋‚˜ ๋ฉ”์†Œ๋“œ๋Š” 2phase์—์„œ๋งŒ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ํ•ด๋‹น ๊ฐ์ฒด์˜ ํ”„๋กœํผํ‹ฐ๋Š” 1phase, 2phase ๋‘˜๋‹ค ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ํ•˜์œ„ ํด๋ž˜์Šค์˜ ๋ชจ๋“  ํ”„๋กœํผํ‹ฐ๊ฐ€ ์ดˆ๊ธฐํ™” ๋˜์ง€ ์•Š์œผ๋ฉด ์ƒ์œ„ํด๋ž˜์Šค์˜ designated initializer๋Š” ํ˜ธ์ถœ๋˜์ง€ ๋ชปํ•œ๋‹ค.
  • ์‹ค์Šต ์ฝ”๋“œ
import UIKit
class Parent {
    var name: String
    init(name: String) {
        self.name = name
    }
    
    func say() {
        print("haha")
    }
}

class Child: Parent {
    var childName: String
    init(childName: String) {
        self.childName = childName
        print(self.childName)
        //----------์—ฌ๊ธฐ๊นŒ์ง€ 1phase
        //์—ฌ๊ธฐ์„œ๋Š” ์ด ๊ฐ์ฒด์˜ ๋ฉ”์†Œ๋“œ์— ์ ‘๊ทผ ๋ถˆ๊ฐ€
        //์ƒ์œ„ ํด๋ž˜์Šค์— ๊ทธ ์–ด๋–ค๊ฒƒ๋„ ์ ‘๊ทผ ๋ถˆ๊ฐ€
        //์˜ค์ง ์ด ๊ฐ์ฒด์˜ ํ”„๋กœํผํ‹ฐ๋งŒ ์ ‘๊ทผ๊ฐ€๋Šฅ
        super.init(name: "papa")
        //-----------------------------
        //----------์—ฌ๊ธฐ๋ถ€ํ„ฐ 2phase
        super.say()
        super.name = "hihi"
        print(self.name)
        self.sayHi()
    }
    
    convenience init(age: Int) {
        self.init(childName: "child")
        self.childName = "hihi"
    }
    
    func sayHi() {
        print("HI")
    }
}

Safety check 1 ์ƒ์œ„ ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•˜๊ธฐ์ „์— ํ•ด๋‹น ํด๋ž˜์Šค์˜ ๋ชจ๋“  ํ”„๋กœํผํ‹ฐ๊ฐ€ ์ดˆ๊ธฐํ™”๋˜์–ด์•ผํ•จ ํด๋ž˜์Šค๊ฐ€ ์ƒ์„ฑ๋  ๋•Œ ๋ชจ๋“  ์ €์žฅ ํ”„๋กœํผํ‹ฐ์˜ ๊ฐ’์„ ์•Œ์•„์•ผ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํ• ๋‹นํ•  ์ˆ˜ ์žˆ์Œ. ๋”ฐ๋ผ์„œ ํ•ด๋‹น ํด๋ž˜์Šค์˜ designated ์ƒ์„ฑ์ž๊ฐ€ ๋ชจ๋“  ์ €์žฅ ํ”„๋กœํผํ‹ฐ์˜ ๊ฐ’์ด ์ดˆ๊ธฐํ™”๋˜์—ˆ๋Š”์ง€ ํŒ๋‹จํ•จ

Safety check 2 ํ•˜์œ„ ํด๋ž˜์Šค์—์„œ ์ƒ์œ„ํด๋ž˜์Šค๋กœ๋ถ€ํ„ฐ ์ƒ์†๋œ ํ”„๋กœํผํ‹ฐ์— ์ ‘๊ทผํ•˜๋ ค๋ฉด designated initializer๋Š” ๋ธ๋ฆฌ๊ฒŒ์ดํŠธ ์—…์œผ๋กœ ์ƒ์œ„ ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž๋ฅผ ๋จผ์ € ํ˜ธ์ถœํ•ด์•ผ ํ•จ. ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด(ํ•˜์œ„ ํด๋ž˜์Šค์—์„œ ์ƒ์œ„ํด๋ž˜์Šค์˜ ํ”„๋กœํผํ‹ฐ ๊ฐ’์„ ํ• ๋‹นํ•˜๋ฉด) ์–ด์ฐจํ”ผ ์ƒ์œ„ํด๋ž˜์Šค์˜ designated initializer์— ์˜ํ•ด ๊ฐ’์ด ๋ฎ์–ด์”Œ์›Œ์ง <- ๊ทผ๋ฐ ์ด๊ฒŒ Xcode์—์„œ ๋ฌธ๋ฒ•์ ์œผ๋กœ ๋ถˆ๊ฐ€๋Šฅํ•จ

Safety check 3 ์ปจ๋น„๋‹ˆ์–ธ์Šค ์ด๋‹ˆ์…œ๋ผ์ด์ €๋Š” ํ”„๋กœํผํ‹ฐ์— ๊ฐ’์„ ํ• ๋‹นํ•˜๊ธฐ ์ „์— ๋ฌด์กฐ๊ฑด ๋‹ค๋ฅธ ์ด๋‹ˆ์…œ๋ผ์ด์ €๋ฅผ ํ˜ธ์ถœํ•ด์„œ ๋ชจ๋“  ํ”„๋กœํผํ‹ฐ์˜ ๊ฐ’์ด ํ• ๋‹น๋˜์–ด์•ผ ํ•œ๋‹ค. ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ํ• ๋‹น๋œ ๊ฐ’์ด ๋‹ค๋ฅธ ์ด๋‹ˆ์…œ๋ผ์ด์ €์— ์˜ํ•ด ๋ฎ์–ด์”Œ์›Œ์ง <- ๊ทผ๋ฐ ์ด๊ฒŒ Xcode์—์„œ ๋ฌธ๋ฒ•์ ์œผ๋กœ ๋ถˆ๊ฐ€๋Šฅํ•จ

Safety check 4 ์ƒ์„ฑ์ž๋Š” 1 phase initialization์ด ์™„๋ฃŒ๋˜๊ธฐ ์ „์— ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ–‰๋™์„ ํ•  ์ˆ˜ ์—†๋‹ค. ์ธ์Šคํ„ด์Šค์˜ ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ, ์ธ์Šคํ„ด์Šค์˜ ํ”„๋กœํผํ‹ฐ ์ ‘๊ทผ, self๋ฅผ ๊ฐ’์œผ๋กœ์จ ์ ‘๊ทผ 1 phase initialization์ด ์™„๋ฃŒ๋œ ์‹œ์ ์€ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๊ฐ€ ์™„์ „ํžˆ ์ƒ์„ฑ๋œ ์‹œ๊ธฐ๊ฐ€ ์•„๋‹ˆ์ง€๋งŒ ์ธ์Šคํ„ด์Šค ํ”„๋กœํผํ‹ฐ์— ์ ‘๊ทผ, ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

Initializer Inheritance and Overriding

  • ํ•˜์œ„ ํด๋ž˜์Šค๊ฐ€ ์ƒ์œ„ ํด๋ž˜์Šค์™€ ๊ฐ™์€ ์ƒ์„ฑ์ž๋ฅผ ๋งŒ๋“ค ๋•Œ override ํ‚ค์›Œ๋“œ๋ฅผ ์จ์•ผํ•œ๋‹ค.
    • ์ด๋Š” ํ•˜์œ„ํด๋ž˜์Šค์˜ ๊ฐ„ํŽธ ์ƒ์„ฑ์ž๊ฐ€ ์ƒ์œ„ ํด๋ž˜์Šค์˜ ์ง€์ • ์ƒ์„ฑ์ž์™€ ๊ฐ™์€ ๊ฒฝ์šฐ์—๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋‹ค.
    • ํ•˜์œ„ํด๋ž˜์Šค์˜ ์ง€์ •์ƒ์„ฑ์ž๊ฐ€ ์ƒ์œ„ํด๋ž˜์Šค์˜ ๊ฐ„ํŽธ์ƒ์„ฑ์ž๊ฐ€ ๊ฐ™์€ ๊ฒฝ์šฐ์—๋Š” overrideํ‚ค์›Œ๋“œ๋ฅผ ์“ธ ํ•„์š”๊ฐ€ ์—†๋‹ค.
    • ํ•˜์œ„ํด๋ž˜์Šค์˜ ๊ฐ„ํŽธ์ƒ์„ฑ์ž๊ฐ€ ํ•˜์œ„ํด๋ž˜์Šค์˜ ์ง€์ •์ƒ์„ฑ์ž๋ฅผ ๋ฐ”๋กœ ํ˜ธ์ถœํ•  ์ˆ˜๊ฐ€ ์—†๊ธฐ๋•Œ๋ฌธ์— overrideํ•˜๋Š”๊ฒŒ ์•„๋‹ˆ๋ผ๊ณ  ํ•จ

๊ฒฝ์šฐ1) ํ•˜์œ„์˜ ์ง€์ • ์ƒ์„ฑ์ž๊ฐ€ ์ƒ์œ„์˜ ์ง€์ •์ƒ์„ฑ์ž๋ฅผ overrideํ•˜๋Š”๊ฒฝ์šฐ -> override ํ‚ค์›Œ๋“œ ํ•„์š”ํ•จ

class Animal {
    let name: String
    
    init(name: String) {
        self.name = name
    }
}

class Dog: Animal {

    override init(name: String) { //override ์•ˆ์“ฐ๋ฉด ์—๋Ÿฌ๋‚จ
        super.init(name: name)
    }
}

๊ฒฝ์šฐ2) ํ•˜์œ„์˜ ๊ฐ„ํŽธ์ƒ์„ฑ์ž๊ฐ€ ์ƒ์œ„์˜ ์ง€์ •์ƒ์„ฑ์ž์™€ ๊ฐ™์€๊ฒฝ์šฐ -> override ํ‚ค์›Œ๋“œ ํ•„์š”ํ•จ

class Animal {
    let name: String

    init(name: String) {
        self.name = name
    }

    init(lastName: String) {
        self.name = "kim" + lastName
    }
}

class Dog: Animal {
    let age: Int

    init(name: String, age: Int) {
        self.age = age
        super.init(name: name)
    }

    override convenience init(lastName: String) {
        self.init(name: "kim" + lastName, age: 30)
    }
}

๊ฒฝ์šฐ3) ํ•˜์œ„์˜ ์ง€์ •์ƒ์„ฑ์ž๊ฐ€ ์ƒ์œ„์˜ ๊ฐ„ํŽธ์ƒ์„ฑ์ž์™€ ๊ฐ™์€ ๊ฒฝ์šฐ -> override ํ‚ค์›Œ๋“œ ํ•„์š”์—†์Œ

class Animal {
    let name: String

    init(name: String) {
        self.name = name
    }

    convenience init(lastName: String) {
        self.init(name: "noName" + lastName)
    }
}

class Dog: Animal {
    let age: Int

    init(lastName: String) { //override ์“ฐ๋ฉด ์—๋Ÿฌ๋‚จ
        self.age = 30
        super.init(name: "pikachu")
    }
}

๊ฒฝ์šฐ3) ํ•˜์œ„์˜ ๊ฐ„ํŽธ์ƒ์„ฑ์ž๊ฐ€ ์ƒ์œ„์˜ ๊ฐ„ํŽธ์ƒ์„ฑ์ž์™€ ๊ฐ™์€๊ฒฝ์šฐ -> override ํ‚ค์›Œ๋“œ ํ•„์š”์—†์Œ

class Animal {
    let name: String

    init(name: String) {
        self.name = name
    }

    convenience init(lastName: String) {
        self.init(name: "noName" + lastName)
    }
}

class Dog: Animal {
    let age: Int

    init(name: String, age: Int) {
        self.age = age
        super.init(name: name)
    }

    convenience init(lastName: String) {//override ํ‚ค์›Œ๋“œ ํ•„์š”์—†์Œ
        self.init(name: "noName" + lastName, age: 30)
    }
}
ํ•˜์œ„ ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž ์ƒ์œ„ ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž override ํ‚ค์›Œ๋“œ ์—ฌ๋ถ€
์ง€์ • ์ง€์ • override ํ•„์š”ํ•จ
๊ฐ„ํŽธ ์ง€์ • override ํ•„์š”ํ•จ
์ง€์ • ๊ฐ„ํŽธ override ํ•„์š”์—†์Œ
๊ฐ„ํŽธ ๊ฐ„ํŽธ override ํ•„์š”์—†์Œ
  • ํ•˜์œ„ ํด๋ž˜์Šค๊ฐ€ phase 2์—์„œ ์•„๋ฌด๋Ÿฐ ํ•  ์ผ์ด ์—†๊ณ (์ƒ์œ„ํด๋ž˜์Šค์˜ ํ”„๋กœํผํ‹ฐ๋ฅผ ์ˆ˜์ •ํ•˜์ง€ ์•Š๊ณ  ํ˜„์žฌ ํด๋ž˜์Šค์˜ ๋ฉ”์†Œ๋“œ์— ์ ‘๊ทผํ•˜์ง€ ์•Š์„๋•Œ), ๊ทธ๋ฆฌ๊ณ  ์ƒ์œ„ ํด๋ž˜์Šค์˜ ์ง€์ • ์ƒ์„ฑ์ž๊ฐ€ argument๊ฐ€ ์—†์„๋•Œ super.init()์„ ์ƒ๋žตํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ์•„๋ž˜๋Š” ์˜ˆ์‹œ
class Vehicle {
    var numberOfWheels = 0
    var description: String {
        return "\(numberOfWheels) wheel(s)"
    }
}

class Hoverboard: Vehicle {
    var color: String
    init(color: String) {
        self.color = color
        // super.init() implicitly called here
        //๋งŒ์•ฝ ์ƒ์œ„ ํด๋ž˜์Šค์˜ ํ”„๋กœํผํ‹ฐ์— ์ ‘๊ทผํ•˜๊ณ  ์‹ถ์œผ๋ฉด super.init()์„ ๋ช…์‹œํ•ด์•ผํ•จ
    }
    override var description: String {
        return "\(super.description) in a beautiful \(color)"
    }
}

Automatic Initializer Inheritance

  • ํ•˜์œ„ ํด๋ž˜์Šค๋Š” ์ƒ์œ„ํด๋ž˜์Šค์˜ ์ง€์ • ์ƒ์„ฑ์ž๋ฅผ ๊ธฐ๋ณธ์ ์œผ๋กœ ์ƒ์†ํ•˜์ง€ ์•Š๋Š”๋‹ค.(ํŠน์ • ์กฐ๊ฑด์—์„œ๋งŒ ์ƒ์†ํ•จ)

  • ํ•˜์œ„ ํด๋ž˜์Šค์—์„œ ์ƒˆ๋กœ์šด ํ”„๋กœํผํ‹ฐ๋ฅผ ๋งŒ๋“ค์—ˆ์„ ๋•Œ ๋‹ค์Œ 2๊ฐ€์ง€ ๋ฃฐ์ด ์ ์šฉ๋จ

    • ์ฒซ๋ฒˆ์งธ ๋ฃฐ
      • ํ•˜์œ„ ํด๋ž˜์Šค์—์„œ ์ง€์ •์ƒ์„ฑ์ž๋ฅผ ๋งŒ๋“ค์ง€ ์•Š์œผ๋ฉด ์ƒ์œ„ ํด๋ž˜์Šค์˜ ๋ชจ๋“  ์ง€์ •์ƒ์„ฑ์ž๊ฐ€ ์ƒ์†๋จ
    • ๋‘๋ฒˆ์งธ ๋ฃฐ
      • ํ•˜์œ„ ํด๋ž˜์Šค์—์„œ ์Šˆํผํด๋ž˜์Šค์˜ ๋ชจ๋“  ์ง€์ • ์ƒ์„ฑ์ž๋ฅผ ๋งŒ๋“ค๋ฉด(์ฒซ๋ฒˆ์žฌ ๋ฃฐ์— ์˜ํ•ด ์ƒ์†๋œ ๊ฒฝ์šฐ ํฌํ•จ) ์ƒ์œ„ ํด๋ž˜์Šค์˜ ๋ชจ๋“  ๊ฐ„ํŽธ ์ƒ์„ฑ์ž๊ฐ€ ์ƒ์†๋จ
      • ์ด๋Š” ์„œ๋ธŒํด๋ž˜์Šค์—์„œ ์ถ”๊ฐ€์ ์ธ ๊ฐ„ํŽธ์ƒ์„ฑ์ž๋ฅผ ๋งŒ๋“  ๊ฒฝ์šฐ๋„ ํฌํ•จ๋œ๋‹ค.

NOTE

A subclass can implement a superclass designated initializer as a subclass convenience initializer as part of satisfying rule 2.