๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป iOSun
article thumbnail

ํด๋กœ์ € (Closures)

  • ์ „์—ญํ•จ์ˆ˜ = ์ด๋ฆ„์„ ๊ฐ€์ง€๊ณ  ์–ด๋– ํ•œ ๊ฐ’๋„ ์บก์ฒ˜ํ•˜์ง€ ์•Š๋Š” ํด๋กœ์ €
  • ์ค‘์ฒฉ ํ•จ์ˆ˜ = ์ด๋ฆ„์„ ๊ฐ€์ง€๊ณ  ๋‘˜๋Ÿฌ์‹ผ ํ•จ์ˆ˜๋กœ ๋ถ€ํ„ฐ ๊ฐ’์„ ์บก์ฒ˜ํ•  ์ˆ˜ ์žˆ๋Š” ํด๋กœ์ €
  • ์ฃผ๋ณ€ ์ปจํ…์ŠคํŠธ์—์„œ ๊ฐ’์„ ์บก์ฒ˜ํ•  ์ˆ˜ ์žˆ๋Š” ์š”์•ฝ๋œ ๊ตฌ๋ฌธ = ์ด๋ฆ„์ด ์—†๋Š” ํด๋กœ์ €(Unnamed Closure)
  • ํด๋กœ์ €์˜ ํŒŒ๋ผ๋ฏธํ„ฐ → inout ํŒŒ๋ผ๋ฏธํ„ฐ ์ผ ์ˆ˜ ์žˆ์ง€๋งŒ ๊ธฐ๋ณธ๊ฐ’์„ ๊ฐ€์งˆ ์ˆ˜ ์—†๋‹ค.
let names = ["Chris", "Alex", "Ewa", "Barry", "Daniella"]

func backward(_ s1: String, _ s2: String) -> Bool {
    return s1 > s2
}
var reversedNames = names.sorted(by: backward)

// backwardํ•จ์ˆ˜๋ฅผ ํด๋กœ์ €๋กœ ํ‘œํ˜„
reversedNames = names.sorted(by: { (s1: String, s2: String) -> Bool in
    return s1 > s2
})

// ์งง์œผ๋ฉด ํ•œ ์ค„๋กœ ํ‘œํ˜„
reversedNames = names.sorted(by: { (s1: String, s2: String) -> Bool in return s1 > s2 } )

// ํƒ€์ž… ์ถ”๋ก  -> ๋ฆฌํ„ด ํƒ€์ž…๊ณผ ํŒŒ๋ผ๋ฏธํ„ฐ ํƒ€์ž… ์ƒ๋žต
reversedNames = names.sorted(by: { s1, s2 in return s1 > s2 } )

// return ํ‚ค์›Œ๋“œ ์ƒ๋žต
reversedNames = names.sorted(by: { s1, s2 in s1 > s2 } )

// Shorthand Argument Names ์‚ฌ์šฉ
reversedNames = names.sorted(by: { $0 > $1 } )

// Operator ๋ฉ”์†Œ๋“œ
reversedNames = names.sorted(by: >)

 

ํ›„ํ–‰ ํด๋กœ์ € (Trailing Closures)

  • ํ•จ์ˆ˜์˜ ๋งˆ์ง€๋ง‰ argument๋กœ ํด๋กœ์ €๋ฅผ ์ „๋‹ฌํ•  ๋•Œ ํ›„ํ–‰ ํด๋กœ์ €๋กœ ์ž‘์„ฑ
func loadPicture(from server: Server, completion: (Picture) -> Void, onFailure: () -> Void) {
    if let picture = download("photo.jpg", from: server) {
        completion(picture)
    } else {
        onFailure()
    }
}

loadPicture(from: someServer) { picture in
    someView.currentPicture = picture
} onFailure: {
    print("Couldn't download the next picture.")
}
  • 2๊ฐœ์˜ ํด๋กœ์ €๋ฅผ ์‚ฌ์šฉ
  • ๋„คํŠธ์›Œํฌ ํ†ต์‹ ์ด ์„ฑ๊ณตํ•œ ๊ฒฝ์šฐ์—์„œ ๋„คํŠธ์›Œํฌ ์˜ค๋ฅ˜ ์ฒ˜๋ฆฌ ์ฝ”๋“œ ๋ถ„๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค.

 

์บก์ฒ˜๊ฐ’ (Capturing Values)

  • ํด๋กœ์ € ์บก์ณ๋ž€ ํด๋กœ์ €๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋‚˜ ์ง€์—ญ๋ณ€์ˆ˜๊ฐ€ ์•„๋‹Œ ์ฃผ๋ณ€ ์™ธ๋ถ€์˜ context๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ์ฃผ๋ณ€ ์™ธ๋ถ€์˜ context๋ฅผ ์ฐธ์กฐํ•˜๋Š” ๊ฒƒ(Capturing by reference)
  • ์ปจํ…์ŠคํŠธ์—์„œ ์ƒ์ˆ˜์™€ ๋ณ€์ˆ˜๋ฅผ ์บก์ฒ˜ (capture) ํ•  ์ˆ˜ ์žˆ๋‹ค.
func makeIncrementer(forIncrement amount: Int) -> () -> Int {
    var runningTotal = 0

    func incrementer() -> Int {
        runningTotal += amount
        return runningTotal
    }

    return incrementer
}

let incrementByTen = makeIncrementer(forIncrement: 10)

incrementByTen()
// returns a value of 10
incrementByTen()
// returns a value of 20
incrementByTen()
// returns a value of 30
  • ์ค‘์ฒฉํ•จ์ˆ˜ & ๋ฆฌํ„ด ํƒ€์ž… = () -> Int → ํ•จ์ˆ˜ ๋ฐ˜ํ™˜ํ•œ๋‹ค๋Š” ์˜๋ฏธ
  • incrementer() ํ•จ์ˆ˜๋Š” runningTotal ๊ณผ amount 2๊ฐœ์˜ ๊ฐ’์„ ์บก์ฒ˜
  • incrementByTen์ด ์ด ๋‘˜์„ ์บก์ณ → ๊ฐ’์ด ๊ณ„์† ์‚ด์•„์žˆ๊ฒŒ ๋˜๊ณ , ๋”ฐ๋ผ์„œ ์ด ํด๋กœ์ €๋ฅผ ํ˜ธ์ถœํ•จ์— ๋”ฐ๋ผ ๊ฐ’์ด updating์ด ๋จ

 

ํด๋กœ์ €๋Š” ์ฐธ์กฐ ํƒ€์ž…

let alsoIncrementByTen = incrementByTen
alsoIncrementByTen()
// returns a value of 40
  • 2๊ฐœ ๋ชจ๋‘ ๊ฐ™์€ ํด๋กœ์ €๋ฅผ ์ฐธ์กฐํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋‘˜๋‹ค ์ฆ๊ฐ€
  • ํด๋กœ์ €์˜ reference count๊ฐ€ 0์ด ๋˜์ง€ ์•Š๋Š” ํ•œ ์บก์ณํ•œ runningTotal์ด๋‚˜ amount๋Š” ์‚ฌ๋ผ์ง€์ง€ ์•Š๋Š”๋‹ค.

 

ํƒˆ์ถœ ํด๋กœ์ € (Escaping Closures)

var completionHandlers: [() -> Void] = []
func someFunctionWithEscapingClosure(completionHandler: @escaping () -> Void) {
    // ํ•จ์ˆ˜ ๋ฐ”๊นฅ์— ์„ ์–ธ๋œ ๋ฐฐ์—ด์— ์ถ”๊ฐ€ -> @escaping ํ‘œ์‹œ ํ•ด์•ผ ํ•จ(์•ˆํ•˜๋ฉด ์ปดํŒŒ์ผ ์—๋Ÿฌ)
		completionHandlers.append(completionHandler)
}
  • @escaping
  • ํด๋กœ์ €๊ฐ€ ํƒˆ์ถœ → ํ•จ์ˆ˜ ๋ฐ”๊นฅ์— ์ •์˜๋œ ๋ณ€์ˆ˜์— ์ €์žฅ๋˜๋Š” ๊ฒƒ
  • self ์บก์ณ → ๊ฐ•ํ•œ ์ฐธ์กฐ ์ˆœํ™˜์ด ์ƒ๊ธธ ์ˆ˜ ์žˆ์Œ
class Class2 {

    var handler: ((Int) -> Void)? = nil
    
    func format(_ value: Int) -> String { return String(value) }

    func test() {
        handler = { [weak self] value in
            let formatted = self?.format(value)
            print("formatted: \\(formatted as Any)")
        }
    }

    deinit {
        print("Class2 deinit")
    }
}
  • ํ•จ์ˆ˜ ์™ธ๋ถ€ ๋ณ€์ˆ˜์— escaping ํด๋กœ์ €๊ฐ€ ์ €์žฅ๋  ๋•Œ๋Š” [weak self]๋ฅผ ์‚ฌ์šฉํ•˜์ž
profile

๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป iOSun

@iosun

ํฌ์ŠคํŒ…์ด ์ข‹์•˜๋‹ค๋ฉด "์ข‹์•„์š”โค๏ธ" ๋˜๋Š” "๊ตฌ๋…๐Ÿ‘๐Ÿป" ํ•ด์ฃผ์„ธ์š”!

profile on loading

Loading...