Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closure & Escaping Closure #11

Closed
5 tasks done
Glsme opened this issue Dec 20, 2022 · 5 comments
Closed
5 tasks done

Closure & Escaping Closure #11

Glsme opened this issue Dec 20, 2022 · 5 comments
Labels

Comments

@Glsme
Copy link
Contributor

Glsme commented Dec 20, 2022

๐Ÿ—“๏ธ ๋งˆ๊ฐ์ผ

2023.01.22

๐Ÿ’ ์ฐธ์—ฌ์ž

  • ๊น€๋„์ด
  • ์œค์—ฌ์ง„
  • ํ™์„์ค€
  • ์ด์ฃผ์˜
  • ์ด์žฌ์šฉ

โš ๏ธ ๋งˆ์ง€๋ง‰์œผ๋กœ ๋‹ต๋ณ€ ๋‹จ ์‚ฌ๋žŒ์ด ์ด์Šˆ close ํ•˜๊ณ  ๋ฆฌ๋“œ๋ฏธ์— ํ•ญ๋ชฉ ์—…๋ฐ์ดํŠธ ํ•ด์ฃผ์„ธ์š”.

@Glsme Glsme added the Swift label Dec 20, 2022
@Yun-YeoJin
Copy link
Contributor

1. ํด๋กœ์ €(Closure)

  • ์‚ฌ์šฉ์ž ์ฝ”๋“œ ์•ˆ์—์„œ ์ „๋‹ฌ๋˜์–ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋กœ์ง์„ ๊ฐ€์ง„ { }๋กœ ๊ตฌ๋ถ„๋œ ์ฝ”๋“œ ๋ธ”๋Ÿญ
  • ๋”ฐ๋กœ function์„ ๋งŒ๋“ค์–ด์•ผ ํ•  ๋ถˆํŽธํ•จ ์—†์ด ์ฆ‰์„์—์„œ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Œ.
  • ํ›„ํ–‰ ํด๋กœ์ €(trailing Closure)๋ฅผ ์ด์šฉํ•ด ๋งˆ์ง€๋ง‰ ๋งค๊ฐœ๋ณ€์ˆ˜ ์ด๋ฆ„์„ ์ƒ๋žตํ•˜๊ณ  { }๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ.
  • ํƒ€์ž… ์ƒ๋žต ๊ฐ€๋Šฅ, return ๊ฐ’์ด ํ•œ ์ค„์ผ๋•Œ return ์ƒ๋žต ๊ฐ€๋Šฅ, ๋งค๊ฐœ๋ณ€์ˆ˜ ์ƒ๋žต ๊ฐ€๋Šฅ($0, $1)
  • ๊ธฐ๋ณธ์ ์œผ๋กœ ํ•จ์ˆ˜์˜ ์ธ์ž๋กœ ๋“ค์–ด์˜ค๋Š” Closure๋Š” ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ๋งŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅ. ํ•จ์ˆ˜ ์™ธ๋ถ€์— ์ €์žฅ ๋ฐ ๋‹ค๋ฅธ ์Šค๋ ˆ๋“œ์—์„œ ํ•ด๋‹น ํด๋กœ์ €๋ฅผ ์‹คํ–‰ ๋ถˆ๊ฐ€ โŒ

2. ํƒˆ์ถœ ํด๋กœ์ €(Escaping Closure)

  • ํด๋กœ์ € ํƒ€์ž… ์•ž์—ย @escapingย ๋ฅผ ๋ถ™์ด๋ฉด ํƒˆ์ถœ ํด๋กœ์ €๋กœ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ๊ณ , ํƒˆ์ถœ ํด๋กœ์ €๋Š” ํ•ด๋‹น ํ•จ์ˆ˜๊ฐ€ ๋ฐ˜ํ™˜๋œ ํ›„ ์‹คํ–‰๋˜๋ฏ€๋กœ, ์ผ๋ฐ˜์ ์ธ ํด๋กœ์ €์™€ ๋‹ฌ๋ฆฌ ํ•จ์ˆ˜ ์™ธ๋ถ€์— ์ €์žฅํ•˜๊ฑฐ๋‚˜ ๋‹ค๋ฅธ ์Šค๋ ˆ๋“œ์—์„œ ์‹คํ–‰ ๊ฐ€๋Šฅ ๐ŸŸข
  • ๊น”๋”ํ•œ ์•„ํ‚คํ…์ฒ˜ ์„ค๊ณ„์—๋„ ๋„์›€์ด ๋จ!!!
  • ํƒˆ์ถœ ํด๋กœ์ €๋Š” ํ•ด๋‹น ํ•จ์ˆ˜๊ฐ€ ๋ฐ˜ํ™˜๋œ ํ›„ ์‹คํ–‰๋˜๊ธฐ ๋•Œ๋ฌธ์— ํ•จ์ˆ˜ ๊ฐ„ ์‹คํ–‰ ์ˆœ์„œ๋„ ๊ณ ๋ คํ•  ์ˆ˜ ์žˆ๋‹ค.

@LeeJoobang
Copy link

LeeJoobang commented Jan 15, 2023

  • ๋จผ์ € ํด๋กœ์ ธ๋Š” ์™œ ์“ฐ๋Š” ๊ฒƒ์ผ๊นŒ? ๋น„๋™๊ธฐ ์ž‘์—…์—์„œ ๋งŽ์ด ์‚ฌ์šฉ๋˜๊ณ  ์žˆ๋‹ค. ๋น„๋™๊ธฐ์ ์ธ ์–ด๋–ค ํ…Œ์Šคํฌ๊ฐ€ ๋๋‚˜๊ณ  ๋‚˜์„œ ์ˆ˜ํ–‰ํ•ด์•ผ ํ•  ๋ธ”๋ก์„ ํด๋กœ์ ธ๋กœ ์ž‘์„ฑํ•˜๋ฉด ํŽธ๋ฆฌํ•˜๋‹ค. ํด๋กœ์ ธ๋Š” ๋น„๋™๊ธฐ ์ž‘์—…์—์„œ ๋งŽ์ด ์‚ฌ์šฉ๋˜๋Š”๋ฐ, ํด๋กœ์ €๋ฅผ ํ†ตํ•ด ๋น„๋™๊ธฐ ์ฝœ๋ฐฑ์„ ์ž‘์„ฑํ•˜๋Š” ๊ฒฝ์šฐ ํ˜„์žฌ ์ƒํƒœ๋ฅผ ๋ฏธ๋ฆฌ ํš๋“ํ•ด ๋†“์ง€ ์•Š์œผ๋ฉด ์‹ค์ œ๋กœ ํด๋กœ์ ธ์˜ ๊ธฐ๋Šฅ์„ ์‹คํ–‰ํ•˜๋Š” ์ˆœ๊ฐ„, ์ƒ์ˆ˜๋‚˜ ๋ณ€์ˆ˜๊ฐ€ ์ด๋ฏธ ๋ฉ”๋ชจ๋ฆฌ์— ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๋•Œ ํ•„์š”ํ•œ ๊ฒƒ์ด ๋ฐ”๋กœ ํด๋กœ์ ธ์˜ ๊ฐ’ ์บก์ณ ๊ธฐ๋Šฅ์ด๋‹ค.

    • ๊ฐ’ ์บก์ณ๋Š” ๋ง ๊ทธ๋Œ€๋กœ ์บก์ณํ•ด์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ๋งํ•œ๋‹ค. ๊ฐ’์„ ์บก์ณํ•ด์„œ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค๊ฐ€. ํ•„์š”ํ•  ๋•Œ๋งˆ๋‹ค ์ด์šฉํ•˜๋Š” ๊ฒƒ์ด๋‹ค.
    • ๋ฆฌํ„ด ํƒ€์ž…์˜ ํ•จ์ˆ˜๊ฐ์ฒด๋ฅผ ์„ ์–ธํ–ˆ์„ ๋•Œ, ์ฃผ์„์„ ํ™•์ธํ•˜๋ฉด ์„ ์–ธํ•  ๋•Œ๋งˆ๋‹ค ๊ฒฐ๊ณผ๊ฐ’์ด ์ฆ๊ฐ€๋œ๋‹ค.
    • total ๋ณ€์ˆ˜๊ฐ€ ์บก์ณ๋˜์–ด ์ƒˆ๋กญ๊ฒŒ ์„ ์–ธ๋ ๋•Œ๋งˆ๋‹ค ์ดˆ๊ธฐํ™”ํ•˜์ง€ ์•Š๊ณ , ๊ทธ์ „์— ์บก์ณ๋œ ๊ฐ’์œผ๋กœ ๊ณ„์†ํ•ด์„œ ์‚ฌ์šฉํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•œ๋‹ค.
    • ์ด๋ ‡๊ฒŒ ๊ฐ’ ์บก์ณ๋ฅผ ํ†ตํ•˜๋ฉด add ํ•จ์ˆ˜๋Š” ์–ธ์ œ ํ˜ธ์ถœ๋˜๋”๋ผ๋„ ์ž์‹ ๋งŒ์˜ total ๋ณ€์ˆ˜๋ฅผ ๊ฐ€์ง€๊ณ  ์นด์šดํŠธํ•˜๊ฒŒ ๋œ๋‹ค. total ์ฐธ์กฐ๋ฅผ ํš๋“ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.
    • ๊ฐ’ ์บก์ณ๋Š” ๊ฐ’์„ ๋ฏธ๋ฆฌ ํš๋“, ์„ ์–ธ๋  ๋•Œ๋งˆ๋‹ค ํš๋“ํ•œ ๊ฐ’์˜ ์ฐธ์กฐ์— ์—ฐ์‚ฐ์„ ๋ˆ„์ ํ•˜๋Š” ๊ฒƒ์ด๋‹ค.
    • ํด๋กœ์ ธ์—์„œ ๊ฐ’์„ ์บก์ณํ•œ๋‹ค๋Š” ๊ฒƒ์€ - ๋‚ด๋ถ€ํ•จ์ˆ˜์™€ ๋‚ด๋ถ€ํ•จ์ˆ˜์— ์˜ํ–ฅ์„ ๋ฏธ์น˜๋Š” ์ฃผ๋ณ€ ํ™˜๊ฒฝ์„ ๋ชจ๋‘ ํฌํ•จํ•œ ๊ฐ์ฒด์ด๋‹ค.
  • ํด๋กœ์ ธ๋ž€ ์ต๋ช…ํ•จ์ˆ˜๋Š” ํด๋กœ์ ธ ๋‚ด๋ถ€์—์„œ ์™ธ๋ถ€๋ณ€์ˆ˜์ธ num์ด๋ผ๋Š” ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋‹ค. num์˜ ๊ฐ’์„ ํด๋กœ์ ธ ๋‚ด๋ถ€์ ์œผ๋กœ ์ €์žฅํ•˜๊ณ  ์žˆ๋Š”๋ฐ, ์ด๊ฒƒ์€ ํด๋กœ์ ธ์— ์˜ํ•ด num์˜ ๊ฐ’์ด ์บก์ณ ๋˜์—ˆ๋‹ค ๋ผ๊ณ  ํ‘œํ˜„ํ•จ.

    • message๋ž€ ๋ณ€์ˆ˜๋Š” ํด๋กœ์ ธ ๋‚ด๋ถ€์—์„œ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ํด๋กœ์ ธ์— ์˜ํ•ด ๊ฐ’์ด ์บก์ณ ๋˜์ง€ ์•Š์Œ.
  • ํด๋กœ์ ธ์˜ ๊ฐ’ ์บก์ณ ๋ฐฉ์‹ - ํด๋กœ์ ธ๋Š” ๊ฐ’์„ ์บก์ณํ•  ๋•Œ, value / reference ํƒ€์ž…์— ๊ด€๊ณ„์—†์ด reference captureํ•œ๋‹ค. ๋ณ€์ˆ˜๋ฅผ ํด๋กœ์ ธ ๋‚ด๋ถ€์ ์œผ๋กœ ์ €์žฅํ•œ๋‹ค๊ณ  ํ–ˆ์Œ. ํด๋กœ์ ธ๋Š” value / reference ํƒ€์ž…์— ๊ด€๊ณ„์—†์ด ์บก์ณํ•˜๋Š” ๊ฐ’๋“ค์„ ์ฐธ์กฐํ•จ. reference capture๋ผ๊ณ  ํ•จ.

    • ํด๋กœ์ ธ๋Š” ์ž„์˜์˜ ์™ธ๋ถ€๋ณ€์ˆ˜๋ฅผ ํด๋กœ์ ธ ๋‚ด๋ถ€์—์„œ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ž„์˜์˜ ์™ธ๋ถ€๋ณ€์ˆ˜๋ฅผ ์บก์ณํ•จ. ์ž„์˜์˜ ์™ธ๋ถ€๋ณ€์ˆ˜๋ฅผ ์ฐธ์กฐํ•œ๋‹ค.
    • ๊ทธ๋Ÿฐ๋ฐ value type์œผ๋กœ ์บก์ณ๋ฅผ ํ•˜๊ณ  ์‹ถ์œผ๋ฉด ์–ด๋–ป๊ฒŒ ํ• ๊นŒ?
    • ์ด๋•Œ ์บก์ณ ๋ฆฌ์ŠคํŠธ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. - ์บก์ณ๋ฆฌ์ŠคํŠธ๋Š” ๊ฒฐ๊ตญ ๊ธฐ๋ณธ์ ์œผ๋กœ reference type์œผ๋กœ ์บก์ณ๋œ ๋‚ด์šฉ์„ value type์œผ๋กœ ์บก์ณํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์–ด๋–ป๊ฒŒ ํ•˜๋ƒ๋ฉด value type์˜ ๊ฒฝ์šฐ value captureํ•˜๊ณ  ์‹ถ์€ ๋ณ€์ˆ˜๋ฅผ ๋ฆฌ์ŠคํŠธ๋กœ ๋ช…์‹œํ•ด์ฃผ๋Š” ๊ฒƒ์ด๋‹ค.
  • ํƒˆ์ถœ ํด๋กœ์ ธ? ํ•จ์ˆ˜์˜ ์ธ์ž๋กœ ์ „๋‹ฌ๋œ ํด๋กœ์ ธ๊ฐ€ ํ•จ์ˆ˜๊ฐ€ ๋ฐ˜ํ™˜๋œ ํ›„ ์‹คํ–‰๋˜๋Š” ํด๋กœ์ ธ๋ฅผ ์˜๋ฏธํ•œ๋‹ค. ์ „๋‹ฌ ๋ฐ›์€ ํด๋กœ์ ธ๊ฐ€ ํด๋กœ์ ธ ํ•จ์ˆ˜ ์™ธ๋ถ€๋กœ ๋‹ค์‹œ ๋ฐ˜ํ™˜๋˜๊ฑฐ๋‚˜ ์™ธ๋ถ€ ๊ธ€๋กœ๋ฒŒ ๋ณ€์ˆ˜์— ์ €์žฅ๋˜๋Š” ๊ฒฝ์šฐ ์ด๊ฒฝ์šฐ @escaping์„ ๋ถ™์—ฌ์ค˜์•ผ ํ•˜๊ณ , ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด complie ํ•œ๋‹ค.

    • ๋จผ์ € ํด๋กœ์ ธ๋Š” 2๊ฐ€์ง€ ์ข…๋ฅ˜๊ฐ€ ์žˆ๋‹ค.
    • non-escaping closure, escaping closure๊ฐ€ ์žˆ๋‹ค.
    • non-escaping closure๋Š” ํด๋กœ์ ธ๊ฐ€ ํ•จ์ˆ˜๊ฐ€ ์ข…๋ฃŒ๋˜๊ธฐ ์ „์— ์‹คํ–‰๋˜๋Š” ๊ฒƒ
    • escaping closure๋Š” ํ•จ์ˆ˜ ์‹คํ–‰ ์ค‘ ์ฆ‰์‹œ ์‹คํ–‰๋˜์ง€ ์•Š๊ณ , url ์š”์ฒญ์ด ๋๋‚œ ํ›„ ๋น„๋™๊ธฐ๋กœ ์‹คํ–‰๋œ๋‹ค.
    • ํด๋กœ์ ธ๊ฐ€ ๋‹ค๋ฅธ ๋ณ€์ˆ˜์— ์ €์žฅ๋˜๊ฑฐ๋‚˜ ๋‚˜์ค‘์— ์‹คํ–‰๋˜๊ฑฐ๋‚˜ ๋น„๋™๊ธฐ๋กœ ์‹คํ–‰๋  ๋•Œ escaping ํด๋กœ์ ธ๊ฐ€ ์‚ฌ์šฉ๋œ๋‹ค.

@heydoy heydoy changed the title Closure & Eascaping Closure Closure & Escaping Closure Jan 23, 2023
@heydoy
Copy link
Member

heydoy commented Jan 23, 2023

Closure

ํด๋กœ์ €๋Š” ์ฝ”๋“œ์—์„œ ์‚ฌ์šฉ๋˜๋ฉฐ, ๋‹ค์–‘ํ•˜๊ฒŒ ์ „๋‹ฌ๋  ์ˆ˜ ์žˆ๋Š” ๋…๋ฆฝ์ ์ธ ๊ธฐ๋Šฅ ๋ธ”๋ก.
Swift์˜ ํด๋กœ์ €๋Š” ์˜ค๋ธŒ์ ํ‹ฐ๋ธŒ C์™€ C์˜ ๋ธ”๋ก, ๋‹ค๋ฅธ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์˜ ๋žŒ๋‹ค์™€ ์œ ์‚ฌ.

ํด๋กœ์ €๋Š” ํด๋กœ์ €๊ฐ€ ์ •์˜๋œ ๋งฅ๋ฝ์—์„œ ์ƒ์ˆ˜์™€ ๋ณ€์ˆ˜์˜ ์ฐธ์กฐ๋ฅผ ์ €์žฅํ•˜๊ณ  ์บก์ณํ•  ์ˆ˜ ์žˆ๋‹ค.

ํด๋กœ์ € ํ‘œํ˜„์‹

{ (parameters) -> return type in
    statements
}

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

ํ•จ์ˆ˜์™€ ํด๋กœ์ €๋Š” ์ฐธ์กฐ ํƒ€์ž…
ํด๋กœ์ €๋ฅผ ์„œ๋กœ ๋‹ค๋ฅธ ์ƒ์ˆ˜, ๋ณ€์ˆ˜์— ํ• ๋‹นํ•˜๋ฉด ๋™์ผํ•œ ํด๋กœ์ €๋ฅผ ์ฐธ์กฐํ•˜๊ณ  ์ฐธ์กฐ ์นด์šดํŠธ๊ฐ€ ์ฆ๊ฐ€.

Escaping Closure

ํƒˆ์ถœ ํด๋กœ์ €๋Š” ํ•จ์ˆ˜์— ๋Œ€ํ•œ ์ธ์ˆ˜๋กœ ์ „๋‹ฌ๋˜๋‚˜, ํ•จ์ˆ˜๊ฐ€ ๋ฐ˜ํ™˜๋œ ์ดํ›„์— ํ˜ธ์ถœ๋œ๋‹ค. @escaping ํ‚ค์›Œ๋“œ๋ฅผ ํด๋กœ์ € ์•ž์— ์ ์–ด ์‚ฌ์šฉํ•œ๋‹ค.

let completionHandler = () -> Void 
func havingEscapingClosure(completionHandler: @escaping () -> Void) {
    completionHandlers.append(completionHandler)
}

@Glsme
Copy link
Contributor Author

Glsme commented Jan 29, 2023

Closure

  • ํด๋กœ์ €๋Š” Named Clousre & Unnamed Closure ๋‘˜๋‹ค ํฌํ•จํ•˜์ง€๋งŒ, ๋ณดํ†ต Unnamed Closure๋ฅผ ๋งํ•œ๋‹ค.
  • ํด๋กœ์ €๋Š” ํ•จ์ˆ˜์ด๊ธฐ ๋•Œ๋ฌธ์— ์ผ๊ธ‰ ๊ฐ์ฒด ํ•จ์ˆ˜์˜ ํŠน์„ฑ์„ ๊ฐ€์ง„๋‹ค.


1๊ธ‰ ๊ฐ์ฒด๋กœ์„œ ํด๋กœ์ € ํŠน์ง•

  • ํด๋กœ์ €๋ฅผ ๋ณ€์ˆ˜๋‚˜ ์ƒ์ˆ˜์— ๋Œ€์ž…ํ•  ์ˆ˜ ์žˆ๋‹ค.
let closure = { () -> () in
    print("Closure")
}
  • ํ•จ์ˆ˜์˜ ํŒŒ๋ผ๋ฏธํ„ฐ ํƒ€์ž…์œผ๋กœ ํด๋กœ์ €๋ฅผ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ๋‹ค.
func doSomething(closure: () -> ()) {
    closure()
}
  • ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜ ํƒ€์ž…์œผ๋กœ ํด๋กœ์ €๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
func doSomething() -> () -> () {
}



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

ํ•จ์ˆ˜ ์ธ์ž๋กœ ํด๋กœ์ €๊ฐ€ ์ „๋‹ฌ๋˜์ง€๋งŒ, ํ•จ์ˆ˜๊ฐ€ ๋ฐ˜ํ™˜๋œ ์ดํ›„์— ์‹คํ–‰๋˜๋Š” ํด๋กœ์ €์ด๋‹ค.

  • ํƒˆ์ถœ ํด๋กœ์ €๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„  ์•ž์— @escaping ํ‚ค์›Œ๋“œ๋ฅผ ๋ถ™์—ฌ์•ผ ํ•œ๋‹ค.
  • ๋ณดํ†ต ๋น„๋™๊ธฐ ์ž‘์—…์„ ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•œ๋‹ค.
  • ์ผ๋ฐ˜ ํด๋กœ์ €์ฒ˜๋Ÿผ ์บก์ฒ˜ ํ˜„์ƒ์„ ๊ฐ€์ง€๊ณ  ์žˆ์–ด, Memory Leak์— ์ฃผ์˜ํ•˜๋ฉฐ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

@wodyddml2
Copy link

Closure

Closure๋Š” ์ต๋ช… ํ•จ์ˆ˜์ด๋ฉฐ ํ•จ์ˆ˜์ด๊ธฐ์— 1๊ธ‰ ๊ฐ์ฒด ํŠน์„ฑ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.

  • 1๊ธ‰ ๊ฐ์ฒด
    • ๋ณ€์ˆ˜์— ํ•จ์ˆ˜ ๋Œ€์ž… ๊ฐ€๋Šฅ
    • ํ•จ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ ๊ฐ€๋Šฅ
    • ์ธ์ž๋กœ ํ•จ์ˆ˜๋ฅผ ์ „๋‹ฌ ๊ฐ€๋Šฅ

๋ฐ˜ํ™˜ ํƒ€์ž…์„ ์ƒ๋žตํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ๋‹จ์ถ• ์ธ์ž ์ด๋ฆ„์„ ์‚ฌ์šฉํ•˜์—ฌ Closure ์ฝ”๋“œ ๋ฐฉ์‹์„ ์ค„์ผ ์ˆ˜ ์žˆ๋‹ค.

Escaping Closure

์ „๋‹ฌ์ธ์ž๋กœ ๋ฐ›์€ ํด๋กœ์ €๊ฐ€ ๋งค์„œ๋“œ ์‹คํ–‰์ด ์ข…๋ฃŒ๋œ ํ›„์— ํ˜ธ์ถœ๋˜๋Š” ๊ฒฝ์šฐ, ํƒˆ์ถœ ํด๋กœ์ €๋ผ๊ณ  ํ•จ.

  • ๋Œ€๋ถ€๋ถ„์˜ ๊ฒฝ์šฐ(ํด๋กœ์ €๋ฅผ ํ”„๋กœํผํ‹ฐ์— ์ €์žฅํ•˜๊ฑฐ๋‚˜, ๋‹ค๋ฅธ ํด๋กœ์ €๋กœ ์ „๋‹ฌํ•˜๋Š” ๊ฒฝ์šฐ) ๋ฉ”๋ชจ๋ฆฌ ๋ฆญ์„ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด weak self ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.
  • escaping closure๊ฐ€ ์•„๋‹ ๊ฒฝ์šฐ ๋Œ€๋ถ€๋ถ„์˜ ๊ฒฝ์šฐ weak self ๋ฅผ ์‚ฌ์šฉํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค
    • GCD, UIView.Animate ์˜ ํด๋กœ์ ธ๋Š” weak self๋ฅผ ์“ธ ํ•„์š”๊ฐ€ ์—†๋‹ค

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants