Published on

๐ŸŽ Swift - ํด๋กœ์ €(Closure).02

Authors
  • avatar
    Name
    ์ด์ฐฝ์ค€
    Twitter

์ด๋ฒˆ์—๋Š” ๋ฉ์น˜๊ฐ€ ํฐ ํด๋กœ์ €๋ผ๋Š” ์นœ๊ตฌ๋ฅผ ๋‹ค์ด์–ดํŠธ ์‹œํ‚ค๋Š” ๋ฐฉ๋ฒ•๊ณผ ์ €๋ฅผ ๊ณค๋ž€ํ•˜๊ฒŒ ๋งŒ๋“  @escaping ํด๋กœ์ €์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค

์šฐ๋ฆฌ ํด๋กœ์ €๋ผ๋Š” ์นœ๊ตฌ๋Š” ๋ฉ์น˜๊ฐ€ ๋„ˆ๋ฌด ํฝ๋‹ˆ๋‹ค.. ์ด์ „ ๊ฒŒ์‹œ๋ฌผ์˜ ์˜ˆ์‹œ๋ฅผ ๋‹ค์‹œ ๊ฐ€์ ธ์™€๋ณผ๊นŒ์š”?

foo(closure: { () -> () in
  print("Hi! I'm Closure")
})

ํด๋กœ์ €๋ฅผ ์ธ์ˆ˜๋กœ ๋ฐ›๋Š” ํ•จ์ˆ˜์˜ ํ˜ธ์ถœ๋ฒ•์ž…๋‹ˆ๋‹ค. ์œ„ ์˜ˆ์‹œ๋Š” ์‚ฌ์‹ค ์•„์ฃผ ๋‹จ์ˆœํ•œ ์นœ๊ตฌ์˜€์ฃ ?

foo(closure)

๋„ค.. ๊ฐ€๋งŒํžˆ ํ•œ๋ฒˆ ๊ด€์ฐฐํ•ด๋ณด๋ฉด ์ด ์นœ๊ตฌ๋Š”

() -> ()

์š” ๋ถ€๋ถ„์ด๋ผ๋˜์ง€..

closure:

์š” ๋ถ€๋ถ„์ด๋ผ๋˜์ง€.. ๋ถˆํ•„์š”ํ•˜๊ฒŒ ๋ช…์‹œ๋˜์–ด ์žˆ๋Š” ์—ฌ๋Ÿฌ ๋ถ€๋ถ„๋“ค์ด ๋ณด์ž…๋‹ˆ๋‹ค. ๋งˆ์น˜ ์ €์˜ ๋ชธ์— ๋ถ™์–ด์žˆ๋Š” ๋ถˆํ•„์š”ํ•œ ์ง€๋ฐฉ๊ฐ™์ฃ . ๐Ÿ˜

๊ทธ๋ž˜์„œ ์ง€๊ธˆ๋ถ€ํ„ฐ๋Š” ํด๋กœ์ €์˜ ์ฒด์ค‘ ๊ฐ๋Ÿ‰๋ฒ•์„ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.


Trailing Closure (ํŠธ๋ ˆ์ผ๋ง ํด๋กœ์ €)

๋˜ ์ƒˆ๋กœ์šด ์šฉ์–ด๊ฐ€ ๋‚˜์™”์ง€๋งŒ ์ดํ•ด๋ฅผ ๋•๊ธฐ ์œ„ํ•œ ๋„๊ตฌ๋กœ์„œ ์ ‘๊ทผํ•ด๋ด…์‹œ๋‹ค. โ€œTrailing = ๋์— ๋ถ™์€โ€ ์ด๋ผ๋Š” ๋œป์ด์ฃ ?

์ด ๊ฒฝ๋Ÿ‰๋ฒ•์€ ํ•จ์ˆ˜์˜ ๋งˆ์ง€๋ง‰ ์ธ์ž๋กœ ํด๋กœ์ €๊ฐ€ ๋“ค์–ด๊ฐ€์žˆ์„ ๋•Œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งˆ์ง€๋ง‰ ํด๋กœ์ €๋ฅผ ๋นผ๋‚ด์„œ ํ•จ์ˆ˜์˜ ๋์— ๊ฐ„๋žตํ™”ํ•ด์„œ ๋ถ™์—ฌ์“ฐ๋Š” ๋ฐฉ๋ฒ•์ด์ฃ . ์ด ๋•Œ, ์ธ์ž์˜ ์ž๋ฆฌ์—์„œ ๋›ฐ์ณ๋‚˜์™”์œผ๋‹ˆ ์ธ์ž๋ช…์€ ์ ์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

์—ญ์‹œ ์˜ˆ์‹œ๊ฐ€ ์ดํ•ด์— ์ง๋นต์ด์ฃ ?

foo(closure: { () -> () in
    print("Hi! I'm Closure")
})

๋„ค ์œ„์˜ ์˜ˆ์‹œ์™€ ๊ฐ™์€ ์˜ˆ์‹œ์ž…๋‹ˆ๋‹ค. function({ โ€ฆ }) ์ด๋ ‡๊ฒŒ ๊ด„ํ˜ธ์•ˆ์— ์ค‘๊ด„ํ˜ธ๊ฐ€ ๋“ค์–ด๊ฐ€์žˆ์–ด์„œ ํ•œ ๋ˆˆ์— ํŒŒ์•…์ด ์–ด๋ ค์šด ์ ์ด ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ Trailing Closure๋ฅผ ์‚ฌ์šฉํ•ด๋ณผ๊ฒŒ์š”!

foo() { () -> () in
  print("Hi! I'm Closure")
}

์ด๋ ‡๊ฒŒ!! function() { โ€ฆ } ํ˜•ํƒœ๋กœ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ๊ณ , closure: ์™€ ๊ฐ™์€ ์ธ์ž๋ช…๋„ ์ƒ๋žตํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ์—ฌ๊ธฐ์—์„œ ์ถ”๊ฐ€๋กœ! ์ธ์ž๊ฐ€ ํด๋กœ์ € ๋‹จ ํ•˜๋‚˜์ผ ๊ฒฝ์šฐ์—๋Š” ()๊นŒ์ง€ ์ƒ๋žต ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

foo { () -> () in
  print("Hi! I'm Closure")
}

์ด๋ ‡๊ฒŒ ๋ง์ด์ฃ !

์ธ์ž๊ฐ€ ์—ฌ๋Ÿฌ๊ฐœ์ธ ๊ฒฝ์šฐ๋„ ํ•œ๋ฒˆ ๋ณผ๊ฒŒ์š”.

func fetchData(success: () -> (), fail: () -> ()) {
  // Tasks
}

์œ„ ํ•จ์ˆ˜๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ํ˜ธ์ถœ๋ ๊ฒ๋‹ˆ๋‹ค.

fetchData(success: { () -> () in
  print("Success")
}, fail: { () -> () in
  print("Fail")
})

์ด ํ˜ธ์ถœ๋ฒ•์— Trailing Closure๋ฅผ ํ™œ์šฉํ•ด๋ณผ๊ฒŒ์š”.

fetchData(success: { () -> () in
  print("Success")
}) { () -> () in
  print("Fail")
}

์ด๋ ‡๊ฒŒ! ๋งˆ์ง€๋ง‰ ํด๋กœ์ € ์ธ์ž๋งŒ ๋’ค๋กœ ์™ ๋บ„ ์ˆ˜ ์žˆ์ฃ .

์ •๋ฆฌํ•ด๋ณผ๊นŒ์š”?

  1. ๋งˆ์ง€๋ง‰ ์ธ์ž๊ฐ€ ํด๋กœ์ €์ธ ๊ฒฝ์šฐ, ํ•ด๋‹น ํด๋กœ์ €๋ฅผ ์ธ์ž์—์„œ ๋นผ๋‚ด์„œ ํ•จ์ˆ˜ ๋’ค์— ๋ถ™์ผ ์ˆ˜ ์žˆ๋‹ค.
  2. ์ด ๋•Œ, ์ธ์ž๋ช…์€ ์ƒ๋žต๋œ๋‹ค.
  3. ๋งŒ์•ฝ ์ธ์ž๊ฐ€ ํด๋กœ์ € ๋‹จ ํ•˜๋‚˜์ผ ๊ฒฝ์šฐ์—๋Š” ํ•จ์ˆ˜ ํ˜ธ์ถœ ๊ตฌ๋ฌธ์ธ () ๋˜ํ•œ ์ƒ๋žตํ•  ์ˆ˜ ์žˆ๋‹ค.

( ) -> ( ) in ์ƒ๋žต

์ž.. ๊ทธ๋Ÿฐ๋ฐ ์‚ฌ์‹ค ์ €ํฌ ๋ˆˆ์— ์ œ์ผ ๊ฑฐ์Šฌ๋ฆฌ๋Š”๊ฑด ์ €๋Ÿฐ ๋งˆ์ด๋„ˆํ•œ ๋ถ€๋ถ„์ด ์•„๋‹™๋‹ˆ๋‹ค. ๋ณธ์ธ์ด ํด๋กœ์ €๋ผ๊ณ  ๋‹น๋‹นํžˆ ์†Œ๋ฆฌ์น˜๊ณ  ์žˆ๋Š” () -> () in ์ด ๋ถ€๋ถ„์ด์ฃ .

๋„ˆ๋ฌด ์ž๊ธฐ์ฃผ์žฅ์ด ๊ฐ•ํ•ด์„œ ํด๋กœ์ € ๊ตฌ๋ฌธ์„ ๋ณด๋ฉด ์ € ๋ถ€๋ถ„๋ถ€ํ„ฐ ๋ˆˆ์ด ๊ฐ‘๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์ € ๋ถ€๋ถ„์„ ์ƒ๋žตํ•ด๋ณผ๊ฒ๋‹ˆ๋‹ค.

์ž ์•„๋ž˜์™€ ๊ฐ™์€ ํ•จ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

func foo(closure: (Int, Int, Int) -> (Int)) {
  closure(1, 2, 3)
}

์ธ์ž๋กœ Intํ˜• 1, 2, 3์„ ๋ฐ›์•„ Intํ˜• Return Type์„ ๋‚ด๋ณด๋‚ด๋Š” ํด๋กœ์ €๋ฅผ ์ธ์ˆ˜๋กœ ๊ฐ–๋Š” ํ•จ์ˆ˜ foo()์ด๋„ค์š”. ์ด ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ๋Š” ์–ด๋–ป๊ฒŒ ํ•ด์•ผํ• ๊นŒ์š”?

foo(closure: {closure: (a: Int, b: Int, c: Int) -> Int in
  return a + b + c
})

ํ˜น์€ Trailing Closure๋ฅผ ์‚ฌ์šฉํ•ด์„œ

foo() { (a: Int, b: Int, c: Int) -> Int in
  return a + b + c
}

์ด๋Ÿฐ์‹์œผ๋กœ ์ฃผ์ €๋ฆฌ์ฃผ์ €๋ฆฌ.. ๊ต‰์žฅํžˆ ๊ธธ์–ด์ง‘๋‹ˆ๋‹ค. ์‹ฌ์ง€์–ด ์œ„ ์˜ˆ์‹œ๋Š” ๊ต‰์žฅํžˆ ๊ฐ„๋‹จํ•œ ์˜ˆ์‹œ์ด์ฃ ? ์‹ค์ „์—์„  ๋”์šฑ ๋ณต์žกํ•ด์ง€๊ณ  ๊ธธ์–ด์งˆ๊ฒ๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์ด ์นœ๊ตฌ๋ฅผ ์ด ๋‹ค์„ฏ ๋‹จ๊ณ„์— ๊ฑธ์ณ ์ถ•์•ฝํ•ด๋ณผ๊ฒŒ์š”!

1. ๋ฐ์ดํ„ฐ ํƒ€์ž…์„ ์ƒ๋žตํ•œ๋‹ค.

๊ฐ€์žฅ ๋งŒ๋งŒํ•œ ๋ฐ์ดํ„ฐ ํƒ€์ž…๋ถ€ํ„ฐ ์ƒ๋žตํ•ด๋ด…์‹œ๋‹ค. Swift๋Š” ํ˜„๋Œ€ ์–ธ์–ด์˜ ๊ธฐ๋ณธ ์†Œ์–‘์ธ โ€œ๋ฐ์ดํ„ฐํƒ€์ž… ์•Œ์•„์„œ ์ฒ˜๋ฆฌํ•˜๊ธฐโ€ ๊ธฐ๋Šฅ์ด ๋‹น์—ฐํžˆ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ์ ๊ทน์ ์œผ๋กœ ํ™œ์šฉํ•œ ๋ฐฉ๋ฒ•์ด์ฃ .

foo(closure: {closure: (a: Int, b: Int, c: Int) -> Int in
  return a + b + c
})

์—ฌ๊ธฐ์„œ Int ํƒ€์ž…๋“ค์„ ์ „๋ถ€ ์ƒ๋žตํ•ฉ๋‹ˆ๋‹ค!

foo(closure: {closure: (a, b, c) in 
  return a + b + c
})

๐Ÿ˜ฎ๐Ÿ˜ฎ๐Ÿ˜ฎ

๋ฒŒ์จ๋ถ€ํ„ฐ ์—„์ฒญ๋‚˜๊ฒŒ ์ค„์–ด๋“ค์—ˆ๋„ค์š”! ๋ฌผ๋ก  ๋ฐ์ดํ„ฐํƒ€์ž…์— ๋Œ€ํ•ด ์—„๊ฒฉํ•˜๊ฒŒ ๊ด€๋ฆฌํ•˜์‹ค๊ฑฐ๋ฉด ํ•˜๋‚˜ํ•˜๋‚˜ ์ ์–ด์ฃผ์‹œ๋Š”๊ฒŒ ์ข‹์Šต๋‹ˆ๋‹ค.

2. ํด๋กœ์ €์˜ ์ธ์ž๋ช…์„ ์ƒ๋žตํ•˜๊ณ  shortand๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

์šฐ์„  shortand๊ฐ€ ๋ฌด์—‡์ด๋ƒ ํ•˜๋ฉด,

($0, $1, $2)

์š” ์นœ๊ตฌ๋“ค์ž…๋‹ˆ๋‹ค. ํŒŒ์ด์ฌ์„ ๋‹ค๋ค„๋ณด์‹  ๋ถ„๋“ค์ด๋ผ๋ฉด ์ต์ˆ™ํ•˜์‹ค๊ฑฐ์—์š”. ๊ฐ๊ฐ (์ฒซ ๋ฒˆ์งธ ์ธ์ž, ๋‘ ๋ฒˆ์งธ ์ธ์ž, ์„ธ ๋ฒˆ์งธ ์ธ์ž) ๋ฅผ ์˜๋ฏธํ•˜์ฃ .

์ด๋ฅผ ์œ„ ์˜ˆ์‹œ์— ํ•œ ๋ฒˆ ์ ์šฉํ•ด๋ณผ๊นŒ์š”?

foo(closure: {closure: (a, b, c) in 
  return a + b + c
})

์จ”์Ÿˆ์ŸŒ~

foo(closure: {
  return $0 + $1 + $2
})

์™€์šฐ.. ์ด์ œ ๋‘ ๋ฒˆ์งธ ๋‹จ๊ณ„์ธ๋ฐ ๋ฒŒ์จ ์—„์ฒญ๋‚˜๊ฒŒ ๋‹จ์ˆœํ•ด์กŒ์–ด์š”.

๋ฌผ๋ก  ๋‘ ๋ฒˆ์งธ ๋ฐฉ๋ฒ•์—๋„ ์œ ์˜ํ•  ์ ์€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ธ๋ฑ์Šค ์ˆ˜์™€ ์ธ์ž์˜ ์ˆ˜๋ฅผ ์ •ํ™•ํžˆ ๋งž์ถฐ์ฃผ์…”์•ผ ํ•ด์š”!

3. ํด๋กœ์ € ๊ตฌ๋ฌธ ์•ˆ์— return๋งŒ ์žˆ์„ ๊ฒฝ์šฐ, ์ด ๋˜ํ•œ ์ƒ๋žตํ•œ๋‹ค.

์ด๊ฑด ๋น ๋ฅด๊ฒŒ ์˜ˆ์‹œ๋กœ ๋ณด๊ณ  ๋„˜์–ด๊ฐ‘์‹œ๋‹ค.

foo(closure: {
  return $0 + $1 + $2
})

์ด๋ ‡๊ฒŒ ํด๋กœ์ € ์•ˆ์— return ๊ตฌ๋ฌธ๋งŒ ์žˆ๋Š” ๊ฒฝ์šฐ์—๋Š”?

foo(closure: {
  $0 + $1 + $2
})

์ด๋ ‡๊ฒŒ return ๋˜ํ•œ ์ƒ๋žตํ•ด์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

4. Trailing Closure

์œ„์—์„œ ๋‹ค๋ฃฌ Trailing Closure๋ฅผ ์ ์šฉํ•ด์ค์‹œ๋‹ค.

foo(closure: {
  $0 + $1 + $2
})

๋งˆ์ง€๋ง‰ ์ธ์ž๋ฅผ ํ•จ์ˆ˜ ๋’ค๋กœ ๋นผ๋ฒ„๋ฆฌ์ฃ !

foo() {
  $0 + $1 + $2
}

ใ…Žใ…Ž;;;

5. ์ธ์ž๊ฐ€ ํด๋กœ์ € ๋‹จ ํ•˜๋‚˜๋ผ๋ฉด? ( )๋„ ์ƒ๋žตํ•œ๋‹ค.

๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์œ„์—์„œ ๋‹ค๋ค˜์—ˆ์ฃ ? ์ธ์ž๊ฐ€ ํด๋กœ์ € ๋‹จ ํ•˜๋‚˜์ด๊ณ , Trailing Closure๋ฅผ ์ ์šฉํ•˜๋ฉด () ์•ˆ์—๋Š” ์•„๋ฌด๊ฒƒ๋„ ๋‚จ์•„์žˆ์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

foo() {
  $0 + $1 + $2
}

์—†์–ด์ ธ๋ผ ( )!

foo {
  $0 + $1 + $2
}

์—ฌ๊ธฐ๊นŒ์ง€! ํ†ตํ†ตํ–ˆ๋˜ ํด๋กœ์ € ์นœ๊ตฌ๊ฐ€ ์ด์‘ค์‹œ๊ฐœ๊ฐ€ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.. ๋“œ๋ผ๋งˆํ‹ฑํ•œ ์—ฐ์ถœ์„ ์œ„ํ•ด ๋น„๊ตํ•ด๋ณผ๊ฒŒ์š”.

foo(closure: {closure: (a: Int, b: Int, c: Int) -> Int in
  return a + b + c
})

์ด ์นœ๊ตฌ๊ฐ€..

foo {
  $0 + $1 + $2
}

์ด๋ ‡๊ฒŒ.. ์ด ์ •๋„๋ฉด ํด๋กœ์ € ์นœ๊ตฌ์˜ ๊ฑด๊ฐ•์„ ๊ฑฑ์ •ํ•ด์•ผ ํ•˜๋Š”๊ฑฐ ์•„๋‹Œ๊ฐ€ ์‹ถ๋„ค์š” ๐Ÿ˜…

๋ฌผ๋ก  ์œ„์˜ ์˜ˆ์‹œ๋Š” ๋งค์šฐ ๊ทน๋‹จ์ ์ธ ๊ฒฝ์šฐ์ž…๋‹ˆ๋‹ค. ์‹ค์ „์—์„œ ์ด๋ ‡๊ฒŒ๊นŒ์ง€ ์ถ•์•ฝ๋˜๋Š” ๊ฒฝ์šฐ๋Š” ๊ฑฐ์˜ ์—†๋‹ค๊ณ  ๋ณด์…”๋„ ๋˜์ง€๋งŒ, ํ•œ๋‘๊ฐ€์ง€๋งŒ ์ ์šฉํ•ด๋„ ๊ฐ€์‹œ์„ฑ์ด ํ›จ์”ฌ ์ข‹์•„์ง€๋‹ˆ ์ ๊ทน์ ์œผ๋กœ ํ™œ์šฉํ•ด์ค์‹œ๋‹ค!