Skip to content

Latest commit

ย 

History

History
38 lines (27 loc) ยท 2 KB

FastEnumeration.md

File metadata and controls

38 lines (27 loc) ยท 2 KB

Fast Enumeration ์ด๋ž€ ๋ฌด์—‡์ธ์ง€ ์„ค๋ช…ํ•˜์‹œ์˜ค.

Refer

Answer

์ •์˜

์ด๋ฆ„๋งŒ ๋“ค์œผ๋ฉด ๊ต‰์žฅํžˆ ์ƒ์†Œํ•œ Fast Enumertaion์€ ์ด๋ฏธ ์ž์ฃผ ์‚ฌ์šฉ์ค‘์ผ ๊ฐ€๋Šฅ์„ฑ์ด ๋†’๋‹ค.

Objective-C์—์„œ๋Š” Fase enumeration, Swift์—์„œ๋Š” NSFastEnumeration ํ”„๋กœํ† ์ฝœ์„ ์ฐพ์•„๋ณด๋ฉด ๋œ๋‹ค.

Fast enumeration์˜ ์ •์˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

Fast enumeration is a language feature that allows you to efficiently and safely enumerate over the contents of a collection using a concise syntax.

Fast enumeration์€ ๊ฐ„๊ฒฐํ•œ ๊ตฌ๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ปฌ๋ ‰์…˜์˜ ๋‚ด์šฉ์„ ํšจ์œจ์ ์ด๊ณ  ์•ˆ์ „ํ•˜๊ฒŒ ์—ด๊ฑฐํ•  ์ˆ˜ ์žˆ๋Š” ์–ธ์–ด ๊ธฐ๋Šฅ์ž…๋‹ˆ๋‹ค.

  • ์ธ์Šคํ„ด์Šค๊ฐ€ ๋‹ค๋ฅธ ๊ฐ์ฒด์˜ ์ง‘ํ•ฉ์ฒด์— ์ ‘๊ทผ์„ ์ œ๊ณตํ•˜๋Š” ํด๋ž˜์Šค๋ฉด NSFastEnumeration protocol ์ ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.
  • Foundation framework์— ์žˆ๋Š” collection classes๋“ค(NSArray, NSDictionary, NSSet)์„ ์˜ˆ๋กœ ๋“ค ์ˆ˜ ์žˆ๋‹ค.

๋ฌธ๋ฒ•

for ( Type newVariable in expression ) { statements }

Type existingItem;
for ( existingItem in expression ) { statements }
  • ๋‘ ํ‘œํ˜„์‹ ๋ชจ๋‘ NSFastEnumeration ํ”„๋กœํ† ์ฝœ์„ ๋”ฐ๋ฅด๋Š” ๊ฐ์ฒด๋ฅผ ์‚ฐ์ถœํ•œ๋‹ค.
  • ๋ฐ˜๋ณต ํฌ์ธํ„ฐ (iterating variable : newVariable, existingItem) ๋Š” ์˜ค๋ธŒ์ ํŠธ์˜ ํ•ญ๋ชฉ ์†Œ์ง„์œผ๋กœ ๋ฃจํ”„๊ฐ€ ๋๋‚˜๋ฉด nil์„, ๋ฃจํ”„๊ฐ€ ์ผ์ฐ ์ข…๋ฃŒ๋˜๋ฉด ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰์— ๊ฐ€๋ฆฌํ‚จ ์•„์ดํ…œ์ด ๋‚จ์•„ ์žˆ๊ฒŒ๋œ๋‹ค.

์žฅ์ 

  • ์—ด๊ฑฐ์— ์žˆ์–ด์„œ NSEnumerator ๊ฐ™์€ ๊ฒƒ๋“ค์„ ์ง์ ‘ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค ๋” ํšจ์œจ์ ์ด๋‹ค.
  • ๋ฌธ๋ฒ•์ด ๊ฐ„๋‹จํ•˜๋‹ค.
  • ์•ˆ์ „ํ•˜๋‹ค.
    • ์—ด๊ฑฐ ์ค‘์— ์ง‘ํ•ฉ์ฒด๋ฅผ ์ˆ˜์ •ํ•˜๋ฉด exception ๋ฐœ์ƒํ•œ๋‹ค. ์—ด๊ฑฐ์ฒด๋ฅผ ๋ณ€ํ˜•์— ํ์‡„์ ์œผ๋กœ ๋งŒ๋“ค๊ธฐ ๋•Œ๋ฌธ์— ์•ˆ์ „ํ•˜๋‹ค.
  • ์ผ๋ฐ˜ for๋ฌธ์ฒ˜๋Ÿผ ๋™์ž‘ํ•˜๊ธฐ ๋•Œ๋ฌธ์— break๋‚˜ continue ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.