[Swift๐Ÿฆฉ] #14 ์ดˆ๊ธฐํ™”

๋˜์ƒยท2022๋…„ 3์›” 31์ผ
0

iOS

๋ชฉ๋ก ๋ณด๊ธฐ
27/42
post-thumbnail
  • ์ดˆ๊ธฐํ™” (Initialization) : ํด๋ž˜์Šค, ๊ตฌ์กฐ์ฒด, ์—ด๊ฑฐํ˜• ์ธ์Šคํ„ด์Šค๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ์ค€๋น„ํ•˜๋Š” ๋‹จ๊ณ„. (์ดˆ๊ธฐ๊ฐ’์„ ์„ค์ •ํ•ด์„œ ๋ฉ”๋ชจ๋ฆฌ์— ํ• ๋‹นํ•˜๊ณ ...)
    • initializer ๋ฅผ ์ •์˜ํ•˜์—ฌ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.
    • ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜์ง€ ์•Š์Œ.
  • ์ดˆ๊ธฐํ™”์™€ ๋ฐ˜๋Œ€๋กœ, ๊ฐ’๊ณผ ์ž์›์˜ ํ•ด์ง€๋ฅผ ์œ„ํ•ด deinitializer ๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

1. ์ €์žฅ ํ”„๋กœํผํ‹ฐ์˜ ์ดˆ๊ธฐ๊ฐ’ ์„ค์ •

-> ์‚ฌ์‹ค ์ƒ ์•„๋ž˜ ๋‚ด์šฉ๋“ค์€, ์ €์žฅ ํ”„๋กœํผํ‹ฐ๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜๋Š” ์ˆœ์„œ๋‚˜... ํƒ€์ž…์— ๋”ฐ๋ผ ์ดˆ๊ธฐํ™” ๋ฐฉ๋ฒ• / ํ”„๋กœ์„ธ์Šค์˜ ์ฐจ์ด๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฒƒ์ด๊ณ  ํ•ต์‹ฌ์€ ์—ฌ๊ธฐ.

  • ์ธ์Šคํ„ด์Šค์˜ ์ €์žฅ ํ”„๋กœํผํ‹ฐ๋Š” ์‚ฌ์šฉํ•˜๊ธฐ ์ „์— ๋ฐ˜๋“œ์‹œ ํŠน์ • ๊ฐ’์œผ๋กœ ์ดˆ๊ธฐํ™” ๋˜์–ด์•ผ ํ•œ๋‹ค.
  • initializer ์—์„œ ํ”„๋กœํผํ‹ฐ์— ๊ฐ’์„ ์„ค์ •ํ•˜๋ฉด, ํ”„๋กœํผํ‹ฐ ์˜ต์ €๋ฒ„๊ฐ€ ํ˜ธ์ถœ๋˜์ง€ ์•Š๋Š”๋‹ค. ๊ฐœ๋ฐœํ•  ๋•Œ ์ƒ๊ฐ ํ•„์š”
  • ๐Ÿ“• enum ์€ ์ผ€์ด์Šค ์ค‘ ํ•˜๋‚˜๋กœ ์ดˆ๊ธฐํ™” ๋˜์–ด์•ผ ํ•จ.

initializer

init() {
    // ๊ฐ€์žฅ ๊ฐ„๋‹จํ•œ ํ˜•ํƒœ. ํŒŒ๋ผ๋ฏธํ„ฐ X, init ํ‚ค์›Œ๋“œ ์‚ฌ์šฉ.
}
struct Fahrenheit {
    var temperature: Double
    init() {
        temperature = 32.0
    }
}
var f = Fahrenheit()
print("๊ธฐ๋ณธ ์˜จ๋„๋Š” \(f.temperature)ยฐF ์ž…๋‹ˆ๋‹ค.")
// Prints "๊ธฐ๋ณธ ์˜จ๋„๋Š” 32.0ยฐF ์ž…๋‹ˆ๋‹ค."

Default Property Values

์œ„์˜ ์˜ˆ์‹œ์ฒ˜๋Ÿผ ํ•ญ์ƒ ๊ฐ™์€ ๊ฐ’์„ ๊ธฐ๋ณธ ๊ฐ’์œผ๋กœ ๊ฐ€์ง€๋Š” ๊ฒฝ์šฐ,
์„ ์–ธ๊ณผ ๋™์‹œ์— ๊ฐ’์„ ํ• ๋‹นํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์ดˆ๊ธฐ๊ฐ’์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

struct Fahrenheit {
    var temperature = 32.0
}



2. Customizing Initializer โญ๏ธโญ๏ธ

  • ์ดˆ๊ธฐ๊ฐ’์„ ํ•จ์ˆ˜์ฒ˜๋Ÿผ ์ž…๋ ฅ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๋ฐ›์•„์„œ ์ปค์Šคํ„ฐ๋งˆ์ด์ง• ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ์™ธ๋ถ€ ์ด๋ฆ„, ๋‚ด๋ถ€ ์ด๋ฆ„ ์‚ฌ์šฉ ๊ฐ€๋Šฅ.

์ดˆ๊ธฐํ™” ํŒŒ๋ผ๋ฏธํ„ฐ

struct Celsius {
    var temperatureInCelsius: Double
    init(fromFahrenheit fahrenheit: Double) {
        temperatureInCelsius = (fahrenheit - 32.0) / 1.8
    }
    init(fromKelvin kelvin: Double) {
        temperatureInCelsius = kelvin - 273.15
    }
    init(_ celsius: Double) {
        temperatureInCelsius = celsius
    }
}
let boilingPointOfWater = Celsius(fromFahrenheit: 212.0) // 100.0
let freezingPointOfWater = Celsius(fromKelvin: 273.15) // 0.0

ํŒŒ๋ผ๋ฏธํ„ฐ ์ด๋ฆ„๊ณผ ์ธ์ž ๋ ˆ์ด๋ธ” ~= ์˜ค๋ฒ„๋กœ๋”ฉ

  • ํ•จ์ˆ˜์ฒ˜๋Ÿผ ๋ฉ”์„œ๋“œ ์ด๋ฆ„์€ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  init ์œผ๋กœ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์—,
  • ์—ฌ๋Ÿฌ init ์„ ๊ตฌ๋ณ„ํ•˜๋Š”๋ฐ ์žˆ์–ด์„œ ์ธ์ž ์ด๋ฆ„๊ณผ ๊ฐœ์ˆ˜๊ฐ€ ์ค‘์š”ํ•œ ์—ญํ• ์„ ํ•œ๋‹ค. (ํ•จ์ˆ˜์˜ ์˜ค๋ฒ„๋กœ๋”ฉ์ฒ˜๋Ÿผ)
  • ์ดˆ๊ธฐํ™” ์‹œ ์ธ์ž ์ด๋ฆ„์„ ์•ˆ๋„ฃ๊ณ  ์‹ถ๋‹ค๋ฉด? ํ•จ์ˆ˜์ฒ˜๋Ÿผ _ ์‚ฌ์šฉํ•˜์ž!
struct Color {
    let red, green, blue: Double
    // ์ธ์ž 3๊ฐœ์ธ init
    init(red: Double, green: Double, blue: Double) {
        self.red   = red
        self.green = green
        self.blue  = blue
    }
    // ์ธ์ž 1๊ฐœ์ธ init
    init(white: Double) {
        red   = white
        green = white
        blue  = white
    }
    // ์ธ์ž 1๊ฐœ์ธ init ์ด๋‚˜ ์œ„์™€ ๋‹ค๋ฆ„.
    // ์ธ์ž ์ด๋ฆ„์ด ๋‹ค๋ฅด๋‹ˆ๊นŒ!!
    init(red: Double) {
        self.red   = red
        green = 0
        blue  = 0
    }
}

Optinal Property Types

  • ๋งŒ์•ฝ ์ดˆ๊ธฐํ™”๊ฐ€ ๋๋‚ฌ์„ ๋•Œ, ๊ฐ’์ด ๋น„์–ด์žˆ์„ ์ˆ˜ ์žˆ๋Š” ์ƒํ™ฉ์ด๋ผ๋ฉด optional ๋กœ ์„ ์–ธํ•˜์ž. ์ž๋™์œผ๋กœ nil ์ด ํ• ๋‹น๋œ๋‹ค.
class SurveyQuestion {
    let text: String
    var response: String?
    init(text: String) {
        self.text = text
    }
    func ask() {
        print(text)
    }
}
let beetsQuestion = SurveyQuestion(text: "How about beets?")
beetsQuestion.ask()
// Prints "How about beets?"
beetsQuestion.response = "I also like beets. (But not with cheese.)"
  • let ์œผ๋กœ ์„ ์–ธ๋˜๋ฉด, ์ดˆ๊ธฐํ™” ๊ณผ์ •์—์„œ๋Š” ๊ฐ’์„ ํ• ๋‹นํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ์ดˆ๊ธฐํ™” ๋œ ํ›„์—๋Š” ์ˆ˜์ •ํ•  ์ˆ˜ ์—†๋‹ค.
  • ํด๋ž˜์Šค ์ธ์Šคํ„ด์Šค์— ์žˆ๋Š” var ์˜ ๊ฒฝ์šฐ ์–ธ์ œ๋“ ์ง€ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ํ•˜์œ„ ํด๋ž˜์Šค์—์„œ๋Š” ์ˆ˜์ •ํ•  ์ˆ˜ ์—†๋‹ค.



3. Default Inititalizer

  • init ์„ ์ •์˜ํ•˜์ง€ ์•Š์•„๋„ ๊ธฐ๋ณธ์ ์œผ๋กœ ํ•˜๋‚˜ ์ œ๊ณตํ•ด ์ค€๋‹ค.
  • ๋‹จ, ๋ชจ๋“  ํ”„๋กœํผํ‹ฐ์— ๊ธฐ๋ณธ๊ฐ’์ด ์„ค์ •๋œ ๊ฒฝ์šฐ์—๋งŒ ์ œ๊ณต!
    • ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ์—†๋Š” ํ˜•ํƒœ๊ฐ€ ์ œ๊ณต๋จ.
class Album {
    let titleSong: String = "INVU"
    var hotSong: String? // ๊ธฐ๋ณธ์ ์œผ๋กœ nil ์ด ์ œ๊ณต๋จ.
    var top100: Bool = true
}

var third = Album()
third.hotSong = "๊ทธ๋Ÿฐ ๋ฐค"

๊ตฌ์กฐ์ฒด ํƒ€์ž…์— ๋Œ€ํ•œ ๋ฉค๋ฒ„๋ณ„ ์ดˆ๊ธฐํ™” ๊ตฌ๋ฌธ

  • ๊ตฌ์กฐ์ฒด ํƒ€์ž…์€ init์„ ์ •์˜ํ•˜์ง€ ์•Š์œผ๋ฉด, ์ž๋™์ ์œผ๋กœ ์ „์ฒด ๋ฉค๋ฒ„์— ๋Œ€ํ•œ init ์ด ์ฃผ์–ด์ง„๋‹ค.
  • ๊ธฐ๋ณธ ๊ฐ’์ด ์ •ํ•ด์ ธ ์žˆ๋Š” ๊ฒฝ์šฐ ์ƒ๋žต์ด ๊ฐ€๋Šฅํ•˜๋‹ค.
struct Size {
    var width = 0.0, height = 0.0
}

// ์•„๋ž˜ 3 ๋ผ์ธ์€ ๋ชจ๋‘ ๊ฐ™์€ ๋œป.
let twoByTwo = Size(width: 2.0, height: 2.0)
let twoByTwo = Size(width: 2.0)
let twoByTwo = Size()



4. ๊ฐ’ ํƒ€์ž…์—์„œ ์ดˆ๊ธฐํ™” ์œ„์ž„(init delegation)

  • ๊ฐ™์€ ๊ฐ’ ํƒ€์ž… ๋‚ด์˜ ์ดˆ๊ธฐํ™” ๊ตฌ๋ฌธ์„ self.init ์œผ๋กœ ๋‹ค๋ฅธ ์ดˆ๊ธฐํ™” ๊ตฌ๋ฌธ์—์„œ ๋ถ€๋ฅผ ์ˆ˜ ์žˆ๋‹ค.
  • ๊ฐ’ ํƒ€์ž…์—์„œ ์ดˆ๊ธฐํ™” ๊ตฌ๋ฌธ์„ ํ•˜๋‚˜ ์ •์˜ํ•˜๋ฉด, ๊ธฐ๋ณธ์œผ๋กœ ์ œ๊ณต๋˜๋Š” ๊ธฐ๋ณธ ์ดˆ๊ธฐํ™” ๊ตฌ๋ฌธ / ๋ฉค๋ฒ„๋ณ„ ์ดˆ๊ธฐํ™” ๊ตฌ๋ฌธ์— ์ ‘๊ทผํ•  ์ˆ˜ ์—†๋‹ค.
  • ๋งŒ์•ฝ ๊ธฐ๋ณธ, ๋ฉค๋ฒ„๋ณ„, ์ปค์Šคํ…€ ์ดˆ๊ธฐํ™” ๊ตฌ๋ฌธ์— ์ „๋ถ€ ์ ‘๊ทผํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด, ์ปค์Šคํ…€ init ์„ 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
    }
    init(center: Point, size: Size) {
        let originX = center.x - (size.width / 2)
        let originY = center.y - (size.height / 2)

        // ํ•˜๋‚˜์˜ init ์—์„œ ๋‹ค๋ฅธ init ํ˜ธ์ถœ ๊ฐ€๋Šฅ.
        // initialiation delegate ๋ผ๊ณ  ๋ถ€๋ฆ„.
        self.init(origin: Point(x: originX, y: originY), size: size)
    }
}



5. ํด๋ž˜์Šค ์ƒ์†๊ณผ ์ดˆ๊ธฐํ™”

  • ๋ชจ๋“  ์ €์žฅ ํ”„๋กœํผํ‹ฐ(์ƒ์†๋œ๊ฑฐ ํฌํ•จ)๋Š” ์ดˆ๊ธฐํ™” ์ค‘์— ๋ฐ˜๋“œ์‹œ ์ดˆ๊ธฐ๊ฐ’์ด ํ• ๋‹น ๋˜์–ด์•ผ ํ•œ๋‹ค.
  • ์ดˆ๊ธฐํ™”๋ฅผ ์œ„ํ•ด์„œ, designated init, convenience init ์„ ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค.

1. Designated init

  • ํด๋ž˜์Šค์˜ ์ฃผ ์ดˆ๊ธฐํ™” ๊ตฌ๋ฌธ
  • ํ•ด๋‹น ํด๋ž˜์Šค์— ์žˆ๋Š” ๋ชจ๋“  ํ”„๋กœํผํ‹ฐ๋ฅผ ์ดˆ๊ธฐํ™”
  • ์ƒ์œ„ ํด๋ž˜์Šค ์ดˆ๊ธฐํ™”๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์ƒ์œ„ ํด๋ž˜์Šค ์ฒด์ธ์„ ์ญ‰~ ํƒ€๊ณ  ์ดˆ๊ธฐํ™” ์ง„ํ–‰
    • ์ดˆ๊ธฐํ™” ํ”„๋กœ์„ธ์Šค์˜ ์ตœ์ƒ์œ„ ํด๋ž˜์Šค(๋”์ด์ƒ ์˜ฌ๋ผ๊ฐˆ ๊ณณ์ด ์—†๋Š”) ๋ฅผ funel ์ง€์ ์ด๋ผ๊ณ  ํ•œ๋‹ค.

-> ํด๋ž˜์Šค์— ๊ฑฐ์˜ ์กด์žฌํ•˜์ง€ ์•Š๊ณ (๊ธฐ๋ณธ init ์ด์šฉ), ์žˆ์œผ๋ฉด ํ•˜๋‚˜๋งŒ ์žˆ๋‹ค.

// ์ €์žฅ ํ”„๋กœํผํ‹ฐ์— ๋Œ€ํ•œ ์ดˆ๊ธฐํ™”๋ฅผ ๋ชจ๋‘ ํ•ด๋‘๊ณ ,
class ViewModel {
    var title: String = "title"
    var content: String = "content"
    var author: String?
}
// ๊ธฐ๋ณธ init ์‚ฌ์šฉ
let vm = ViewModel()
  • ํด๋ž˜์Šค๋Š” ์ ์–ด๋„ ํ•˜๋‚˜์˜ designated init ์„ ๊ฐ€์ ธ์•ผ ํ•œ๋‹ค.
    • ์ƒ์œ„ ํด๋ž˜์Šค์—์„œ designated init ์„ ์ƒ์†ํ•˜๋Š” ๊ฒƒ๋„ ๊ฐ€๋Šฅ.
init(parameters) {
    //statements
}

2. Convenience init

  • ๋ณด์กฐ ์ดˆ๊ธฐํ™” ๊ตฌ๋ฌธ
  • designated init ์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ ์„ค์ •ํ•˜์—ฌ, ๋™์ผํ•œ ํด๋ž˜์Šค์˜ designated init ๋ฅผ ํ˜ธ์ถœํ•˜๋„๋ก ์ •์˜ํ•œ๋‹ค.
  • ํŠน์ • ์ผ€์ด์Šค๋‚˜ ์ž…๋ ฅ ๊ฐ’ ํƒ€์ž…์— ๋Œ€ํ•œ ํด๋ž˜์Šค ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๊ธฐ๋„ ํ•œ๋‹ค.

-> ๋Œ€์ฒด๋กœ ์ œ๊ณตํ•˜์ง€ ์•Š๊ณ , ์‹œ๊ฐ„์ด ์ ˆ์•ฝ๋˜๊ฑฐ๋‚˜ ๋” ๋ช…ํ™•ํ•œ ์˜๋ฏธ๋ฅผ ์ง€๋‹๋•Œ๋งŒ ๋งŒ๋“ ๋‹ค.

convenience init(author: String) {
    self.init() // ๊ฐ™์€ ๊ณ„์ธต ๋‚ด์˜ desinated init ์„ ๋ถ€๋ฅธ๋‹ค.
    self.author = author
}

let vm = ViewModel(author: "์ต๋ช…์˜ ์ฝ”๋ผ๋ฆฌ")
convenience init(parameters) {
    // statements
}

3. Initializer Delegation for Class Types โญ๏ธ

  1. designated init ์€ ์ƒ์œ„ ํด๋ž˜์Šค์˜ designated init ์„ ํ•ญ์ƒ ๋ถˆ๋Ÿฌ์•ผ ํ•จ. (Base Class ๋ผ๋ฉด ์˜ˆ์™ธ)
  2. convenience init ์€ ๊ฐ™์€ ํด๋ž˜์Šค ๋‚ด์˜ ๋‹ค๋ฅธ init ์„ ํ•ญ์ƒ ๋ถˆ๋Ÿฌ์•ผ ํ•จ.
  3. convenience init ์€ ๊ถ๊ทน์ ์œผ๋กœ ๊ฐ™์€ ํด๋ž˜์Šค ๋‚ด์˜ ๋‹ค๋ฅธ designated init ์„ ํ•ญ์ƒ ๋ถˆ๋Ÿฌ์•ผ ํ•จ.

๊ฒฐ๋ก  : desinated init ์€ ์ƒ์œ„ ํด๋ž˜์Šค๋กœ, convenience ๋Š” ๊ฐ™์€ ํด๋ž˜์Šค๋กœ init ์„ ์œ„์ž„ํ•œ๋‹ค.


4. 2๋‹จ๊ณ„ ์ดˆ๊ธฐํ™” โญ๏ธ

  • ์ดˆ๊ธฐํ™”๋ฅผ ์•ˆ์ „ํ•˜๊ฒŒ ์ˆ˜ํ–‰ํ•˜๊ณ , ํด๋ž˜์Šค์— ์œ ์—ฐ์„ฑ์„ ์ œ๊ณตํ•˜๊ธฐ ์œ„ํ•ด ์ˆ˜ํ–‰ํ•œ๋‹ค.
    • ํ”„๋กœํผํ‹ฐ ๊ฐ’์ด ์ดˆ๊ธฐํ™” ๋˜๊ธฐ ์ „์— ํ”„๋กœํผํ‹ฐ์— ์ ‘๊ทผํ•˜๋Š” ๊ฒƒ์„ ๋ง‰๊ณ ,
    • ๋‹ค๋ฅธ ์ดˆ๊ธฐํ™” ๊ตฌ๋ฌธ์ด ์˜ˆ์ƒ์น˜ ๋ชปํ•œ ๋‹ค๋ฅธ ๊ฐ’์„ ์„ค์ •ํ•˜๋Š” ๊ฒƒ์„ ๋ง‰๋Š”๋‹ค.

๋‹จ๊ณ„

  1. ๊ฐ stored property ๊ฐ€ ํ•ด๋‹น ํ”„๋กœํผํ‹ฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ํด๋ž˜์Šค์— ์˜ํ•ด ์ดˆ๊ธฐ๊ฐ’ ํ• ๋‹น
  2. (๋ชจ๋“  ์ €์žฅ ํ”„๋กœํผํ‹ฐ์˜ ๊ฐ’์ด ๊ฒฐ์ •๋˜๋ฉด) ๊ฐ ํด๋ž˜์Šค์— ์ €์žฅ๋œ ํ”„๋กœํผํ‹ฐ๋ฅผ ์ถ”๊ฐ€๋กœ ์ปค์Šคํ…€ํ•  ์ˆ˜ ์žˆ์Œ.

NOTE

  • Obj-c ์˜ ๊ฒฝ์šฐ๋Š” ๋ชจ๋“  ํ”„๋กœํผํ‹ฐ์— 0 or null ์„ ๋„ฃ๊ณ  / ์ปค์Šคํ…€ ํ•˜๋Š” ๊ฒƒ์— ๋ฐ˜ํ•ด
  • Swift ๋Š” ๋ชจ๋“  ํ”„๋กœํผํ‹ฐ์— ์‚ฌ์šฉ์ž ์ •์˜ ์ดˆ๊ธฐ๊ฐ’์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์–ด์„œ, 0 or nil ์„ ๊ฐ€์ง€์ง€ ์•Š๋Š” ํƒ€์ž…์— ๋Œ€์ฒ˜ํ•  ์ˆ˜ ์žˆ๋‹ค.

์•ˆ์ „ ์ ๊ฒ€

  1. designated init ์€ ์ƒ์œ„ ํด๋ž˜์Šค์˜ init ์— ์œ„์ž„ํ•˜๊ธฐ ์ „์—, ํด๋ž˜์Šค์— ์ถ”๊ฐ€๋œ ๋ชจ๋“  ํ”„๋กœํผํ‹ฐ๊ฐ€ ์ดˆ๊ธฐํ™” ๋˜์—ˆ๋Š”์ง€ ํ™•์ธ.
  2. designated init ์€ ์ƒ์†๋œ ํ”„๋กœํผํ‹ฐ์— ๊ฐ’์„ ํ• ๋‹นํ•˜๊ธฐ ์ „์—, ์ƒ์œ„ ํด๋ž˜์Šค ์ดˆ๊ธฐํ™” ๊ตฌ๋ฌธ์— ์œ„์ž„. ์•ˆ๊ทธ๋Ÿฌ๋ฉด ๋ถ€๋ชจ์˜ ๊ฐ’์— ๋ฎ์–ด์“ฐ์ž„
  3. convenience init ์€ ๋ชจ๋“  ํ”„๋กœํผํ‹ฐ์— ๊ฐ’์„ ํ• ๋‹นํ•˜๊ธฐ ์ „์— ๋‹ค๋ฅธ ์ดˆ๊ธฐํ™” ๊ตฌ๋ฌธ์— ์œ„์ž„. ์•ˆ๊ทธ๋Ÿผ designated ์— ๋ฎ์–ด์“ฐ์—ฌ์ง.
  4. ์ดˆ๊ธฐํ™” ๊ตฌ๋ฌธ์€ ์ฒซ๋ฒˆ์งธ ์ดˆ๊ธฐํ™”๊ฐ€ ์™„๋ฃŒ๋˜๊ธฐ ์ „๊นŒ์ง€ ์ธ์Šคํ„ด์Šค ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ, ํ”„๋กœํผํ‹ฐ ๊ฐ’ ์ฝ๊ฑฐ๋‚˜, self ๊ฐ’์„ ์ฐธ์กฐํ•  ์ˆ˜ ์—†๋‹ค.
init() {
    // ์ƒ์†๋˜์ง€ ์•Š์€ ๋ชจ๋“  ํ”„๋กœํผํ‹ฐ ์ดˆ๊ธฐํ™”
    
    super.init() // ์ƒ์†๋œ ๊ฐ’ ์„ค์ •๋จ
    
    // ์ƒ์†๋ฐ›์€ ๋ชจ๋“  ํ”„๋กœํผํ‹ฐ ์ดˆ๊ธฐํ™”
}

convenience init() {
    self.init() // designated init
    
    // ์ดˆ๊ธฐํ™” ํ•˜๊ณ  ์‹ถ์€ ๊ฐ’ ์ดˆ๊ธฐํ™”.
}

1 ๋‹จ๊ณ„

  1. class ์—์„œ designated / convenience init ํ˜ธ์ถœ
  2. class ์— ์ƒˆ๋กœ์šด ์ธ์Šคํ„ด์Šค์— ๋Œ€ํ•œ ๋ฉ”๋ชจ๋ฆฌ ํ• ๋‹น. (์•„์ง ์ดˆ๊ธฐํ™”๋Š” X)
  3. designated init ์ด ๋ชจ๋“  stored property ๊ฐ€ ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š”์ง€ ํ™•์ธ -> stored property ์— ๋Œ€ํ•œ ๋ฉ”๋ชจ๋ฆฌ ์ดˆ๊ธฐํ™”
  4. designated init ์€ ์ƒ์œ„ ํด๋ž˜์Šค ์ดˆ๊ธฐํ™” ๊ตฌ๋ฌธ์— ์ดˆ๊ธฐํ™” ์œ„์ž„.
  5. ๊ณ„์† ์œ„์ž„ํ•ด์„œ ์ตœ์ƒ์œ„ ์ฒด์ธ๊นŒ์ง€ ์˜ฌ๋ผ๊ฐ.
  6. ์ตœ์ƒ์œ„ ์ฒด์ธ์—์„œ ๋ชจ๋“  stored property ๊ฐ€ ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค๊ณ  ํ™•์ธํ•˜๋ฉด, ์ธ์Šคํ„ด์Šค์˜ ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ์™„๋ฒฝํ•˜๊ฒŒ ์ดˆ๊ธฐํ™” ๋œ ๊ฒƒ์œผ๋กœ ๊ฐ„์ฃผ.

2 ๋‹จ๊ณ„

  1. ์ฒด์ธ์˜ ์ตœ์ƒ์œ„์—์„œ ํ•˜๋‚˜์”ฉ ์•„๋ž˜๋กœ ๋‚ด๋ ค๊ฐ€๋ฉด, designated init ์—์„œ ์ถ”๊ฐ€๋กœ ์‚ฌ์šฉ์ž๊ฐ€ ์›ํ•˜๋Š” ๋Œ€๋กœ ์ธ์Šคํ„ด์Šค์— ๊ฐ’์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. self ๋กœ ์ ‘๊ทผํ•˜์—ฌ ํ”„๋กœํผํ‹ฐ๋ฅผ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ๊ณ , ์ธ์Šคํ„ด์Šค ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ์Œ.
  2. ์ฒด์ธ์— ๋ชจ๋“  convenience init ์€ ์‚ฌ์šฉ์ž๊ฐ€ ์›ํ•˜๋Š” ๋Œ€๋กœ ์ธ์Šคํ„ด์Šค์— ๊ฐ’์„ ์ง€์ •ํ•˜๊ณ , self ๋กœ ์ž‘์—…ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋จ.

์—ฌ๊ธฐ๋ถ€ํ„ฐ ๋‚ด์ผ!!

Override init

  • Swift ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ์ƒ์œ„ ํด๋ž˜์Šค์˜ init ์„ ์•ˆ์ „ํ•˜๊ณ  ์ ์ ˆํ•œ ๊ฒฝ์šฐ์—๋งŒ ์ƒ์†ํ•˜๊ธฐ ๋•Œ๋ฌธ์—, (๋ถ€๋ชจ ํด๋ž˜์Šค์˜ ๊ฐ„๋‹จํ•œ init ์ด ํ•˜์œ„ ํด๋ž˜์Šค์˜ ๋ชจ๋“  ์ธ์ž๋ฅผ ํฌํ•จํ•˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ์œผ๋‹ˆ๊นŒ.)
  • override init ์œผ๋กœ ์ง์ ‘ ์ƒ์œ„ ํด๋ž˜์Šค์˜ designated init ์„ ์˜ค๋ฒ„๋ผ์ด๋”ฉํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ์ƒ์œ„ ํด๋ž˜์Šค์˜ convenience init ์€ ํ•˜์œ„ ํด๋ž˜์Šค์—์„œ ํ˜ธ์ถœ๋  ์ˆ˜ ์—†์Œ. ๊ฐ™์€ ๊ฒƒ์„ ํ•˜์œ„ํด๋ž˜์Šค์—์„œ ๊ตฌํ˜„ํ•˜๋„๋ผ๋„ override๋ฅผ ์“ธ ํ•„์š”๊ฐ€ ์—†๋‹ค.
  • ์ƒ์œ„ ํด๋ž˜์Šค์˜ ์ž๋™์œผ๋กœ ์ œ๊ณต๋˜๋Š” init ์„ ์‚ฌ์šฉํ•  ๋•Œ๋„ override ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•จ.
  • ๋ฉ”์„œ๋“œ, ์„œ๋ธŒ์Šคํฌ๋ฆฝํŠธ์™€ ๋™์ผํ•˜๊ฒŒ ์ƒ์œ„ ํด๋ž˜์Šค์˜ ๊ฒƒ์„ ํ™•์ธ.
class Vehicle {
    var numberOfWheels = 0 // stored property ์— ๋Œ€ํ•œ ๊ธฐ๋ณธ init ์ œ๊ณต
    var description: String {
        return "\(numberOfWheels) wheel(s)"
    }
}

class Bicycle: Vehicle {
    // Vehicle ์˜ ๊ธฐ๋ณธ init ๊ณผ ๊ฐ™๊ธฐ ๋•Œ๋ฌธ์—,
    // override ๋ฅผ ๋ถ™์—ฌ์•ผ ํ•œ๋‹ค.
    override init() {
        super.init() // ๋ถ€๋ชจ๊บผ ๋จผ์ € ์„ธํŒ…ํ•˜๊ณ ,
        numberOfWheels = 2 // ์ž์‹๊ป„ ์„ธํŒ…ํ•ด์•ผ ๋ฎ์–ด์“ฐ์ด์ง€ ์•Š๋Š”๋‹ค.
    }
}


class Hoverboard: Vehicle {
    var color: String
    
    // ์ดˆ๊ธฐํ™” ๊ตฌ๋ฌธ์—์„œ, color ๋ผ๋Š” ์ด ํด๋ž˜์Šค์— ์กด์žฌํ•˜๋Š” property ๋งŒ ์ดˆ๊ธฐํ™”ํ•˜๊ณ ,
    // ์ƒ์† ๋ฐ›์€ numberOfWheels ๋Š” Vehicle ๊ฒƒ์„ ๋”ฐ๋ผ๊ฐˆ ๊ฒƒ์ž„.
    // ๊ทธ๋ ‡๋‹ค๋ฉด, super.init() ์€ ์ž๋™์œผ๋กœ ํ˜ธ์ถœ๋จ.
    init(color: String) {
        self.color = color
        // super.init() implicitly called here
    }
    override var description: String {
        return "\(super.description) in a beautiful \(color)"
    }
}

Automatic init ์ƒ์†

  • ํ•˜์œ„ ํด๋ž˜์Šค๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ์ƒ์œ„ ํด๋ž˜์Šค์˜ init ์„ ์ƒ์†ํ•˜์ง€ ์•Š์ง€๋งŒ,
  • ํŠน์ • ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋ฉด ์ƒ์†ํ•œ๋‹ค.
      1. ํ•˜์œ„ ํด๋ž˜์Šค๊ฐ€ designated init ์„ ์ •์˜ํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ
        ์ƒ์œ„ ํด๋ž˜์Šค์— ์ง€์ •๋œ ๋ชจ๋“  designated init ์ƒ์†
      1. ํ•˜์œ„ ํด๋ž˜์Šค๊ฐ€ ์ƒ์œ„(super) ํด๋ž˜์Šค์˜ designated init ์„ ๋ชจ๋‘ ๊ตฌํ˜„ํ•œ ๊ฒฝ์šฐ (== ๊ทœ์น™ 1)
        ์ž๋™์œผ๋กœ ์ˆ˜ํผํด๋ž˜์Šค์˜ convenience init์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค

์ƒ์œ„ ํด๋ž˜์Šค์˜ designated init ์„ override ํ•˜์—ฌ ํ•˜์œ„ ํด๋ž˜์Šค์˜ convenience init ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. (์ด๋ ‡๊ฒŒ ๊ตฌํ˜„ํ•ด๋„ ๋ถ€๋ชจ์˜ designated init ์„ ๋ชจ๋‘ ๊ตฌํ˜„ํ•œ ๊ฒƒ์ด๋ฏ€๋กœ ๊ทœ์น™ 2๋ฅผ ๋งŒ์กฑ)


Designated, Convenience init ์˜ ๋™์ž‘

Food > RecipeIngredient > ShoppingListItem ์œผ๋กœ ์ƒ์†๋˜๋Š” ํด๋ž˜์Šค์˜ ์˜ˆ์‹œ๋ฅผ ๋ณผ ๊ฒƒ์ž„.

class Food {
    var name: String
    
    // ํด๋ž˜์Šค๋Š” memberwise init ์ด ๊ธฐ๋ณธ์œผ๋กœ ์ œ๊ณต๋˜์ง€ ์•Š์œผ๋ฏ€๋กœ,
    // designated init ์„ ๊ตฌํ˜„ํ•œ๋‹ค.
    // ๋ถ€๋ชจ ํด๋ž˜์Šค๊ฐ€ ์—†์œผ๋ฏ€๋กœ, super.init() ์€ ๊ตฌํ˜„ํ•  ํ•„์š”๊ฐ€ ์—†์Œ.
    init(name: String) {
        self.name = name
    }
    // convenience init -> designated init
    convenience init() {
        self.init(name: "[Unnamed]")
    }
}

let meat = Food(name: "Bacon")
let mystery = Food()
// Food ๋ฅผ ์ƒ์†.
class RecipeIngredient: Food {
    var quantity: Int
    
    // designated init
    init(name: String, quantity: Int) {
        // ์•ˆ์ „ ์ ๊ฒ€ 1 ์ถฉ์กฑ.
        self.quantity = quantity // ์ž์‹์—๋งŒ ์žˆ๋Š” ๊ฒƒ์„ ๋จผ์ € ์ดˆ๊ธฐํ™”ํ•˜๊ณ 
        super.init(name: name) // ๋ถ€๋ชจ ์ดˆ๊ธฐ์ž ํ˜ธ์ถœ.
    }
    // convenience init. 
    // Food ์˜ designated init ๊ณผ ๊ฐ™์€ ํŒŒ๋ผ๋ฏธํ„ฐ. == override
    override convenience init(name: String) {
        self.init(name: name, quantity: 1)
    }
}

// ๋ถ€๋ชจ์˜ convenience init (๋ถ€๋ชจ์˜ ๋ชจ๋“  designated init ๊ตฌํ˜„ํ•ด์„œ ์ž๋™ ์ƒ์†)
let oneMysteryItem = RecipeIngredient() 

// ๋ถ€๋ชจ์˜ designated init ์„ override ํ•˜์—ฌ convenience init ์œผ๋กœ ๊ตฌํ˜„ํ•จ.
let oneBacon = RecipeIngredient(name: "Bacon") 

// ์ž์‹์˜ designated init
let sixEggs = RecipeIngredient(name: "Eggs", quantity: 6)
class ShoppingListItem: RecipeIngredient {
    var purchased = false // ์ดˆ๊ธฐํ™” ๊ตฌ๋ฌธ ๋Œ€์‹  ์ดˆ๊ธฐ๊ฐ’์„ ๋„ฃ์Œ.
    var description: String {
        var output = "\(quantity) x \(name)"
        output += purchased ? " โœ”" : " โœ˜"
        return output
    }
    
    // ์ดˆ๊ธฐํ™” ๊ตฌ๋ฌธ์ด ์•„์˜ˆ ์—†๊ธฐ ๋•Œ๋ฌธ์—,
    // ์ƒ์œ„ ํด๋ž˜์Šค์˜ ๋ชจ๋“  designated init, convenience init ์„ ์ƒ์†.
}

// ์ƒ์†๋ฐ›์€ 3๊ฐ€์ง€ init ์„ ๋ชจ๋‘ ์‚ฌ์šฉ๊ฐ€๋Šฅ.
var breakfastList = [
    ShoppingListItem(),
    ShoppingListItem(name: "Bacon"),
    ShoppingListItem(name: "Eggs", quantity: 6),
]

breakfastList[0].name = "Orange juice"
breakfastList[0].purchased = true
for item in breakfastList {
    print(item.description)
}

// 1 x Orange juice โœ”
// 1 x Bacon โœ˜
// 6 x Eggs โœ˜



6. Failable init โญ๏ธ

  • ์‹คํŒจํ•  ์ˆ˜ ์žˆ๋Š” init init?
  • ์œ ํšจํ•˜์ง€ ์•Š์€ ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐ’, ์™ธ๋ถ€ ๋ฆฌ์†Œ์Šค ๋ถ€์žฌ, ... ์— ์˜ํ•ด ์‹คํŒจํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ๋™์ผํ•œ ์ด๋ฆ„๊ณผ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ์‹คํŒจ ๊ฐ€๋Šฅ, ์‹คํŒจ ๋ถˆ๊ฐ€๋Šฅ ์ดˆ๊ธฐํ™”๋ฅผ ์ž‘์„ฑํ•  ์ˆœ ์—†๋‹ค.
  • ์ด ๊ตฌ๋ฌธ์ด ์‹คํŒจํ•˜๋ฉด ํ•ด๋‹น ์ธ์Šคํ„ด์Šค๊ฐ€ nil ์ด ๋œ๋‹ค.
struct Animal {
    let species: String
    init?(species: String) {
        // ์ดˆ๊ธฐํ™”๊ฐ€ ์‹คํŒจํ•˜๋Š” ์ˆœ๊ฐ„์— return nil ์„ ํ•˜๋ฉด ๋œ๋‹ค.
        // ์ง„์งœ๋กœ init ๊ตฌ๋ฌธ์ด nil ์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒƒ์€ ์•„๋‹ˆ๊ณ ,
        // ์ด ๊ตฌ๋ฌธ์ด ์‹คํŒจํ•˜๋ฉด ํ•ด๋‹น ์ธ์Šคํ„ด์Šค๊ฐ€ nil ์ด ๋œ๋‹ค.
        if species.isEmpty { return nil }
        self.species = species
    }
}

let anonymousCreature = Animal(species: "")
// anonymousCreature is of type Animal?, not Animal

if anonymousCreature == nil {
    print("The anonymous creature could not be initialized")
}
// Prints "The anonymous creature could not be initialized"

Failable init for Enum

enum ์—์„œ๋„ failable init ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

enum TemperatureUnit {
    case kelvin, celsius, fahrenheit
    init?(symbol: Character) {
        switch symbol {
        case "K":
            self = .kelvin
        case "C":
            self = .celsius
        case "F":
            self = .fahrenheit
        default:
            return nil
        }
    }
}

Failable init for Enum with Raw Values

  • ์›์‹œ๊ฐ’์ด ์žˆ๋Š” enum ์€ ์ ์ ˆํ•œ ์›์‹œ๊ฐ’ ํƒ€์ž…์˜ rawValue ๋ผ๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ,
  • ์ผ์น˜ํ•˜๋Š” ์›์‹œ๊ฐ’์˜ ์ผ€์ด์Šค๊ฐ€ ์žˆ์œผ๋ฉด ํ•ด๋‹น ์ผ€์ด์Šค๋ฅผ ์„ ํƒ, ์—†์œผ๋ฉด nil ์ด ๋˜๋Š” ๊ธฐ๋ณธ failable init ์„ ๊ฐ€์ง„๋‹ค.
enum TemperatureUnit: Character {
    case kelvin = "K", celsius = "C", fahrenheit = "F"
}

let fahrenheitUnit = TemperatureUnit(rawValue: "F") // .kelvin
let unknownUnit = TemperatureUnit(rawValue: "X") // nil

Propagation of Initialization Failure

  • ์‹คํŒจ ๊ฐ€๋Šฅํ•œ ์ดˆ๊ธฐํ™” ์—ญ์‹œ ๋‹ค๋ฅธ ์‹คํŒจ ๊ฐ€๋Šฅํ•œ ์ดˆ๊ธฐํ™”์— ์œ„์ž„ ๊ฐ€๋Šฅ.
  • ํ•˜์œ„ ํด๋ž˜์Šค์˜ ๊ฒƒ๋„ ์ƒ์œ„ ํด๋ž˜์Šค๋กœ ์œ„์ž„ ๊ฐ€๋Šฅ.
  • ์‹คํŒจ๋˜๋ฉด ์ดˆ๊ธฐํ™” ํ”„๋กœ์„ธ์Šค ์ „์ฒด๊ฐ€ ์‹คํŒจ๋˜๊ณ  ๋”์ด์ƒ ์ดˆ๊ธฐํ™” ์ฝ”๋“œ๊ฐ€ ์‹คํ–‰๋˜์ง€ ์•Š๋Š”๋‹ค.
class Product {
    let name: String
    init?(name: String) {
        if name.isEmpty { return nil }
        self.name = name
    }
}

class CartItem: Product {
    let quantity: Int
    init?(name: String, quantity: Int) {
        if quantity < 1 { return nil }
        self.quantity = quantity
        super.init(name: name)
    }
}

// quantity ๊ฐ€ 1 ๋ฏธ๋งŒ์ด๊ฑฐ๋‚˜ name ์ด ๋นˆ ๊ฒƒ์„ ๋„ฃ๊ณ  ์ดˆ๊ธฐํ™”ํ•˜๋ฉด ์‹คํŒจ.

Overriding a Failable Initializer

  • ์‹คํŒจ ๊ฐ€๋Šฅํ•œ ์ดˆ๊ธฐ์ž๋„ ํ•˜์œ„ ํด๋ž˜์Šค์—์„œ ์˜ค๋ฒ„๋ผ์ด๋”ฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.
  • ์‹คํŒจ ๊ฐ€๋Šฅํ•œ ์ดˆ๊ธฐ์ž๋ฅผ ์–ธ๋ž˜ํ•‘ํ•˜์—ฌ ์‹คํŒจ ๋ถˆ๊ฐ€๋Šฅํ•œ ์ดˆ๊ธฐ์ž๋กœ ์žฌ์ •์˜ ํ•  ์ˆ˜ ์žˆ์Œ. ๋ฐ˜๋Œ€๋Š” ์•ˆ๋จ.
  • ๋‹น์—ฐํžˆ ๋ถ€๋ชจ๊ฐ€ ์‹คํŒจ๋ฉด ์ž์‹๋„ ์‹คํŒจ!
class Document {
    var name: String?
    // this initializer creates a document with a nil name value
    init() {}
    // this initializer creates a document with a nonempty name value
    init?(name: String) {
        if name.isEmpty { return nil }
        self.name = name
    }
}

class UntitledDocument: Document {
    override init() {
        // "" ์œผ๋กœ ํ˜ธ์ถœ๋˜์ง€ ์•Š์„ ๊ฒƒ์ž„์ด ๋ณด์žฅ๋จ -> !
        super.init(name: "[Untitled]")!
    }
}

init!

  • ์•”์‹œ์ ์œผ๋กœ ์–ธ๋ž˜ํ•‘๋œ Optional ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜๋Š” failable init.
  • init? <-> init! ์„œ๋กœ์„œ๋กœ ์œ„์ž„, ์˜ค๋ฒ„๋ผ์ด๋“œ๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค.
  • ํ•˜์ง€๋งŒ init -> init! ์— ์œ„์ž„ํ•˜๋ฉด ์ดˆ๊ธฐํ™”๊ฐ€ ์‹คํŒจํ•œ๋‹ค.



7. Required init

  • ๋ชจ๋“  ํ•˜์œ„ ํด๋ž˜์Šค๊ฐ€ ๊ตฌํ˜„ํ•ด์•ผํ•˜๋Š” ์ดˆ๊ธฐํ™” ๊ตฌ๋ฌธ.
  • ํ•˜์œ„ ํด๋ž˜์Šค์—์„œ override ํ‚ค์›Œ๋“œ ์“ธ ํ•„์š” ์—†๋‹ค.
  • ์ƒ์†๋œ ์ดˆ๊ธฐํ™” ๊ตฌ๋ฌธ์œผ๋กœ ์š”๊ตฌ์‚ฌํ•ญ ์ถฉ์กฑ์ด ๊ฐ€๋Šฅํ•˜๋‹ค๋ฉด, ๋ช…์‹œ์ ์œผ๋กœ ๊ตฌํ˜„ํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค.
class SomeClass {
    required init() {
        // initializer implementation goes here
    }
}

class SomeSubclass: SomeClass {
    required init() {
        // subclass implementation of the required initializer goes here
    }
}



8. ํด๋กœ์ €๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๊ธฐ๋ณธ ํ”„๋กœํผํ‹ฐ ๊ฐ’ ์„ค์ • โญ๏ธ

  • ๊ธฐ๋ณธ ๊ฐ’์„ ์„ค์ •ํ•  ๋•Œ, ํด๋กœ์ €๋‚˜ ์ „์—ญ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ์ธ์Šคํ„ด์Šค๊ฐ€ ์ดˆ๊ธฐํ™” ๋  ๋•Œ, ํด๋กœ์ €/ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋˜๊ณ , ๋ฐ˜ํ™˜ ๊ฐ’์ด ํ”„๋กœํผํ‹ฐ์˜ ๊ธฐ๋ณธ ๊ฐ’์œผ๋กœ ํ• ๋‹น๋œ๋‹ค.
class VC: UIViewController {
    let label: UILabel = {
        let l = UILabel()
        l.text= "text"
        l.textColor = .blue
        return l
    }()
}



์š”์•ฝ

  • struct, enum, class ์˜ ์ดˆ๊ธฐํ™”์—์„œ ์ค‘์š”ํ•œ ๊ฒƒ์€
  • ํ•ด๋‹น ํƒ€์ž…์ด ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๋ชจ๋“  stored property (์ƒ์†๋œ ๊ฒƒ ํฌํ•จ) ์— ์ดˆ๊ธฐ๊ฐ’์ด ๋“ค์–ด๊ฐ”๋Š”๊ฐ€?

-> ๊ทธ๊ฒƒ์„ ์‚ฌ์šฉ์ž๊ฐ€ ์ง์ ‘ ํ•˜๊ธฐ ์œ„ํ•ด์„œ initializer๋ฅผ ์ œ๊ณตํ•˜๋Š”๋ฐ...

  • desingated init / convenience init ์œผ๋กœ ์ด๋ฃจ์–ด์ ธ์žˆ๊ณ ,
  • ์•„๋ž˜์ฒ˜๋Ÿผ convenience init ์€ ๊ฐ™์€ class ์˜ desingated init ์œผ๋กœ ์ด์–ด์ ธ์•ผํ•˜๊ณ ,
  • designated init ์€ ์ƒ์œ„ ํด๋ž˜์Šค์˜ desingated init ์„ ํ˜ธ์ถœํ•œ๋‹ค.

  • ์ด ๋•Œ, ์•ˆ์ „ํ•˜๊ฒŒ ์ดˆ๊ธฐํ™”๋ฅผ ํ•˜๊ธฐ ์œ„ํ•ด 2๋‹จ๊ณ„ ์ดˆ๊ธฐํ™”๋ฅผ ์ˆ˜ํ–‰ํ•˜๊ฒŒ ๋˜๊ณ ,
      1. ๋ชจ๋“  stored property ๊ฐ€ ํ•ด๋‹น ํ”„๋กœํผํ‹ฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” class์— ์˜ํ•ด ์ดˆ๊ธฐํ™” ๋˜๊ณ ,
      1. ๊ฐ ํด๋ž˜์Šค์— ์ €์žฅ๋œ property ๋ฅผ custom
  • ์ด ๋‘ ๋‹จ๊ณ„์—์„œ ์•ˆ์ „์„ฑ์„ ํ™•๋ณดํ•˜๊ธฐ ์œ„ํ•ด ์•ˆ์ •์„ฑ ๊ฒ€์‚ฌ๋ฅผ ํ•˜๊ฒŒ ๋œ๋‹ค.
    ์•„๋ž˜์ฒ˜๋Ÿผ๋งŒ ์ž‘์„ฑํ•ด์ฃผ๋ฉด ๋ฌธ์ œ X!!
init() {
    // ์ƒ์†๋˜์ง€ ์•Š์€ ๋ชจ๋“  ํ”„๋กœํผํ‹ฐ ์ดˆ๊ธฐํ™”
    
    super.init() // ์ƒ์†๋œ ๊ฐ’ ์„ค์ •๋จ
    
    // ์ƒ์†๋ฐ›์€ ๋ชจ๋“  ํ”„๋กœํผํ‹ฐ ์ดˆ๊ธฐํ™”
}

convenience init() {
    self.init() // designated init
    
    // ์ดˆ๊ธฐํ™” ํ•˜๊ณ  ์‹ถ์€ ๊ฐ’ ์ดˆ๊ธฐํ™”.
}

์งˆ๋ฌธ

Convinience init์— ๋Œ€ํ•ด ์„ค๋ช…ํ•˜์‹œ์˜ค.

  • Convinience init ์€ ํŽธ๋ฆฌํ•œ ์ดˆ๊ธฐํ™”๋กœ, ๊ฐ™์€ ํด๋ž˜์Šค ๋‚ด์— ์ •์˜๋œ designated init ์„ ํ˜ธ์ถœํ•œ ํ›„, ์ดˆ๊ธฐํ™” ํ•˜๊ณ  ์‹ถ์€ ๊ฐ’์„ ์ดˆ๊ธฐํ™” ํ•˜๋Š” ๊ตฌ๋ฌธ์„ ์ž‘์„ฑํ•˜์—ฌ ๊ตฌํ˜„ํ•œ๋‹ค.
    • ๋ชจ๋“  ์ดˆ๊ธฐํ™” ๊ตฌ๋ฌธ์„ ์—ฌ๊ธฐ์„œ ์“ฐ๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ๋‹ค๋ฅธ init ์„ ๋นŒ๋ ค์„œ ์ด์šฉ.
  • Convinience init ์€ ๊ฐ™์€ ํด๋ž˜์Šค ๋‚ด ๋‹ค๋ฅธ Convinience init ์ด๋‚˜ Designated init ์„ ํ˜ธ์ถœํ•˜๋ฉฐ, ๊ถ๊ทน์ ์œผ๋กœ๋Š” ๊ผญ Designated init ์„ ํ˜ธ์ถœํ•ด์•ผ ํ•œ๋‹ค.
  • ๋งŒ์•ฝ, ํด๋ž˜์Šค์— designated init ์ด ์—†๊ฑฐ๋‚˜, ์ƒ์œ„ ํด๋ž˜์Šค์˜ designated init ์„ ๋ชจ๋‘ ๊ตฌํ˜„ํ•ด์„œ ์•”๋ฌต์ ์œผ๋กœ ์ƒ์œ„ ํด๋ž˜์Šค์˜ init ์ด ๋ชจ๋‘ ์ƒ์†๋˜๋Š” ๊ฒฝ์šฐ์—, ์ƒ์œ„ ํด๋ž˜์Šค์˜ designated init ์„ ํ•˜์œ„ ํด๋ž˜์Šค์˜ convenience init ์œผ๋กœ override ํ•  ์ˆ˜๋„ ์žˆ๋‹ค.



์ฐธ๊ณ 

https://bbiguduk.gitbook.io/swift/language-guide-1/initialization

profile
0๋…„์ฐจ iOS ๊ฐœ๋ฐœ์ž์ž…๋‹ˆ๋‹ค.

0๊ฐœ์˜ ๋Œ“๊ธ€