๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’ป Programming ๊ฐœ๋ฐœ/๐ŸŽ iOS ๊ฐœ๋ฐœ, Swift

defer๊ฐ€ ํ˜ธ์ถœ๋˜๋Š” ์ˆœ์„œ / defer๊ฐ€ ํ˜ธ์ถœ๋˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ

by kimdee 2023. 2. 15.
๋ฐ˜์‘ํ˜•

 

defer ๊ตฌ๋ฌธ์€, defer ๊ตฌ๋ฌธ์ด ์žˆ๋Š” ๊ณณ์—์„œ, ํ”„๋กœ๊ทธ๋žจ ์ปจํŠธ๋กค์„ ์Šค์ฝ”ํ”„ ๋ฐ”๊นฅ์œผ๋กœ ์˜ฎ๊ธฐ๊ธฐ ๋ฐ”๋กœ ์ง์ „์— ์‹คํ–‰ํ•˜๋ ค๋Š” ์ฝ”๋“œ์— ์‚ฌ์šฉํ•œ๋‹ค.

defer {
    // statements
}

defer  ๋ฌธ ์•ˆ์— ์žˆ๋Š” ๋‚ด์šฉ์€ ํ”„๋กœ๊ทธ๋žจ ์ œ์–ด๊ฐ€ ์–ด๋–ป๊ฒŒ ์ด๋™๋˜๋Š”์ง€์™€๋Š” ๊ด€๊ณ„ ์—†์ด ์‹คํ–‰๋œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์ˆ˜๋™์ ์œผ๋กœ ์ž์›๊ด€๋ฆฌ๊ฐ€ ๋“ค์–ด๊ฐˆ ๋•Œ, ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒ๋˜๋”๋ผ๋„ ์•ก์…˜์„ ์ˆ˜ํ–‰ํ•ด์•ผ ํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.

Defer๊ฐ€ ์‹คํ–‰๋˜๋Š” ์ˆœ์„œ

์—ฌ๋Ÿฌ๊ฐœ์˜ defer๋ฌธ์ด ์žˆ์„ ๋•Œ

๊ฐ™์€ ์Šค์ฝ”ํ”„ ๋‚ด์— ์—ฌ๋Ÿฌ๊ฐœ์˜ defer ๋ฌธ์ด ์žˆ์œผ๋ฉด, ์‹คํ–‰๋˜๋Š” ์ˆœ์„œ๋Š” ๋ณด์ด๋Š” ์ˆœ์„œ์˜ ์—ญ์ˆœ. ๋งˆ์ง€๋ง‰ defer ๋ฌธ์„ ์ฒซ๋ฒˆ์งธ๋กœ ์‹คํ–‰ํ•œ๋‹ค๋Š” ์˜๋ฏธ๋Š”, ๋งˆ์ง€๋ง‰ defer ๋ฌธ ์•ˆ์˜ ๋‚ด์šฉ์€ ๋‹ค๋ฅธ defer ๋ฌธ์— ์˜ํ•ด์„œ ์ •๋ฆฌ๋˜๋Š” ๋ฆฌ์†Œ์Šค๋ฅผ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๋œป.

func ๋””ํผํ•จ์ˆ˜() {
    defer { print("์ฒซ๋ฒˆ์งธ") }
    defer { print("๋‘๋ฒˆ์งธ") }
    print("๋งˆ์ง€๋ง‰")
}
๋””ํผํ•จ์ˆ˜()
// ๋งˆ์ง€๋ง‰
// ๋‘๋ฒˆ์งธ
// ์ฒซ๋ฒˆ์งธ

defer๋ฌธ์ด ์ค‘์ฒฉ๋˜์–ด ์žˆ์„ ๋•Œ

์ค‘์ฒฉ๋˜์–ด ์žˆ์„ ๊ฒฝ์šฐ๋„ ๊ฐ€์žฅ ๋ฐ”๊นฅ์ชฝ์— ์žˆ๋Š” defer๋ถ€ํ„ฐ ์‹คํ–‰๋œ๋‹ค.

func ์ค‘์ฒฉ๋””ํผํ•จ์ˆ˜() {
    defer {
        defer {
            defer {
                print("์ค‘์ฒฉ ์ฒซ๋ฒˆ์งธ")
            }
            print("์ค‘์ฒฉ ๋‘๋ฒˆ์งธ")
        }
        print("์ค‘์ฒฉ ๋งˆ์ง€๋ง‰")
    }
}

์ค‘์ฒฉ๋””ํผํ•จ์ˆ˜()
// ์ค‘์ฒฉ ๋งˆ์ง€๋ง‰
// ์ค‘์ฒฉ ๋‘๋ฒˆ์งธ
// ์ค‘์ฒฉ ์ฒซ๋ฒˆ์งธ

์ผ๋ฐ˜์ ์ธ ์šฉ๋ก€

์ปจํ…์ŠคํŠธ๋ฅผ ์—ด๊ฑฐ๋‚˜ ๋‹ซ์„ ๋•Œ

๊ฐ€์žฅ ์ผ๋ฐ˜์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ์ผ€์ด์Šค๋Š” ์Šค์ฝ”ํ”„ ์•ˆ์—์„œ ์ปจํ…์ŠคํŠธ๋ฅผ ์—ด๊ฑฐ๋‚˜ ๋‹ซ์„ ๋•Œ. ์˜ˆ๋ฅผ ๋“ค์–ด์„œ ํŒŒ์ผ ์ ‘๊ทผ์— ๋Œ€ํ•ด ๋‹ค๋ฃฐ ๊ฒฝ์šฐ.  FileHandle ์€ ํ•œ ๋ฒˆ ์ ‘๊ทผ์ด ๋๋‚˜๋ฉด ๋ฐ˜๋“œ์‹œ ๋‹ซ์•„์ค˜์•ผํ•˜๋Š”๋ฐ ์ด๋•Œ defer ๊ตฌ๋ฌธ์„ ์ด์šฉํ•ด ์žŠ์ง€ ์•Š๊ณ  ๋‹ซ์•„์ค„ ์ˆ˜ ์žˆ๋‹ค.

func writeFile() {
    let file: FileHandle? = FileHandle(forReadingAtPath: filepath)
    defer { file?.closeFile() }

    // ํŒŒ์ผ๊ด€๋ จ๋œ ์ฝ”๋“œ๋“ค ๋“ฑ๋“ฑ 
}

๊ฒฐ๊ณผ๊ฐ’

์ปดํ”Œ๋ฆฌ์…˜ ์ฝœ๋ฐฑ์—์„œ ๋ฐ˜ํ™˜๋˜๋Š” ๊ฒฐ๊ณผ๊ฐ’ ๊ด€๋ จํ•˜์—ฌ ์‚ฌ์šฉ.

func getData(completion: (_ result: Result<String>) -> Void) {
    var result: Result<String>?

    defer {
        guard let result = result else {
            fatalError("We should always end with a result")
        }
        completion(result)
    }

    // ๊ฒฐ๊ณผ๊ฐ’ ์ƒ์„ฑํ•˜๊ธฐ 
}

์ด๋ ‡๊ฒŒ ํ•ด์„œ ๊ฒฐ๊ณผ๊ฐ’์„ ํ•ญ์ƒ ๊ฒ€์ฆํ•˜๊ณ  ์ปดํ”Œ๋ฆฌ์…˜ ํ•ธ๋“ค๋Ÿฌ๋ฅผ ์‹คํ–‰ํ•˜๊ฒŒ ํ•จ. ๊ฒฐ๊ณผ๊ฐ’์ด nil ์ผ ๊ฒฝ์šฐ, fatalError ๊ฐ€ ๋˜์ ธ์ง€๊ณ  ์•ฑ์ด fail๋จ

๋กœ๋”ฉ ์ธ๋””์ผ€์ดํ„ฐ

func performLogin() {
    shouldShowProgressView = true

    defer {
        shouldShowProgressView = false
    }

    do {
        let _ = try await LoginManager.performLogin()

        DispatchQueue.main.async {
            self.coordinator?.successfulLogin()
        }

    } catch {
        let error = error
        showErrorMessage = true

defer๊ฐ€ ์‹คํ–‰๋˜์ง€ ์•Š๋Š” ์ƒํ™ฉ

defer๋ฌธ์ด ์ฝํžˆ๊ธฐ ์ „์— ์ข…๋ฃŒ๊ฐ€ ๋˜๋Š” ์ƒํ™ฉ์ผ ๋•Œ

import UIKit

var greeting = "Hello, playground"

func deferSometimeRun(value: Int?) {
    guard let value = value else {
        print("nil์ด๋ผ์„œ ์ข…๋ฃŒ")
        return
    }
    
    defer {
        print("#DEFER: ๊ฐ’์ด \\(value) ์ผ ๋•Œ ์‹คํ–‰๋์–ด์š”.")
    }
    
    print("๊ฐ’ : \\(value)")
}

deferSometimeRun(value: nil)
deferSometimeRun(value: 6)

// nil์ด๋ผ์„œ ์ข…๋ฃŒ
// ๊ฐ’ : 6
// #DEFER: ๊ฐ’์ด 6 ์ผ ๋•Œ ์‹คํ–‰๋์–ด์š”.
func deferAlwaysRun(value: Int?) {
    defer {
        print("#DEFER: ๊ฐ’์ด \\(value) ์ผ ๋•Œ ์‹คํ–‰๋์–ด์š”.")
    }
    
    guard let value = value else {
        print("nil์ด๋ผ์„œ ์ข…๋ฃŒ")
        return
    }

    print("๊ฐ’ : \\(value)")
}

deferAlwaysRun(value: nil)
deferAlwaysRun(value: 7)

// nil์ด๋ผ์„œ ์ข…๋ฃŒ
// #DEFER: ๊ฐ’์ด nil ์ผ ๋•Œ ์‹คํ–‰๋์–ด์š”.
// ๊ฐ’ : 7
// #DEFER: ๊ฐ’์ด Optional(7) ์ผ ๋•Œ ์‹คํ–‰๋์–ด์š”.

๋•Œ๋ฌธ์— defer ๊ตฌ๋ฌธ์€ ํ•ญ์ƒ ํ•ด๋‹น ์Šค์ฝ”ํ”„ ๊ฐ€์žฅ ์œ—์ชฝ์— ํ•ด๋‘๋Š” ๊ฒƒ์„ ๊ถŒ์žฅ. (์™œ๋ƒ๋ฉด defer๋ฌธ ์“ฐ๋Š” ๊ฒƒ ์ž์ฒด๊ฐ€ ์ข…๋ฃŒ๋ฅผ ์ง€์—ฐํ•˜๊ณ  ๋งˆ์ง€๋ง‰์œผ๋กœ ์‹คํ–‰ํ•˜๋Š” ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— )


์ฐธ๊ณ ๋งํฌ

Statements - The Swift Programming Language (Swift 5.7)

A complete guide to the Swift defer statement - LogRocket Blog

Defer usage in Swift

 

 

 


 

 

์˜ค๋Š˜๋„ ์ฝ์–ด์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค. 

 

๊ถ๊ธˆํ•˜๊ฑฐ๋‚˜ ๋‚˜๋ˆ„๊ณ  ์‹ถ์€ ์–˜๊ธฐ๊ฐ€ ์žˆ์œผ์‹œ๋ฉด ๋Œ“๊ธ€๋กœ ์•Œ๋ ค์ฃผ์„ธ์š”!

์žฌ๋ฐŒ๊ฒŒ ์ฝ์œผ์…จ๋‹ค๋ฉด ๊ณต๊ฐ๊ณผ ๊ตฌ๋…์€ ํฐ ํž˜์ด ๋ฉ๋‹ˆ๋‹ค. 

 

ํ•ญ์ƒ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค.

 

์ด ๊ธ€์€ doy.oopy.io ์—๋„ ๋ฐœํ–‰๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. 

 

 

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€