๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐ŸŽ iOS/Swift

[Swift] enumerated()

์•ˆ๋…•ํ•˜์„ธ์š” ์ œ์ธ์ž…๋‹ˆ๋‹ค!

์˜ค๋Š˜์€ enumerated()์— ๋Œ€ํ•ด ์ •๋ฆฌํ•ด๋ณด๋ ค๊ณ  ํ•˜๋Š”๋ฐ์š”, ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ๋ฅผ ํ’€๋‹ค ๋‹ค๋ฅธ ํ’€์ด๋ฅผ ์ฐธ๊ณ ํ•ด๋ณด๋ฉด

enumerated()๋ฅผ ์‚ฌ์šฉํ•ด ๋” ๊น”๋”ํ•˜๊ฒŒ ์ฒ˜๋ฆฌํ•œ ํ’€์ด๊ฐ€ ๋งŽ์•„์„œ enumerated() ํ•จ์ˆ˜๊ฐ€ ๋ฌด์—‡์ธ์ง€,

๊ทธ๋ฆฌ๊ณ  ์–ด๋–ป๊ฒŒ ํ™œ์šฉํ•˜๋Š”์ง€๊นŒ์ง€ ํ•œ๋ฒˆ ์ •๋ฆฌํ•ด๋ณด๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค! ์ œ๋Œ€๋กœ ์ •๋ฆฌํ•ด๋‘๊ณ  ์ž˜ ์จ๋จน์–ด๋ด…์‹œ๋‹ค!

enumerated()

enumerate๋Š” ์‚ฌ์ „์ ์œผ๋กœ ์—ด๊ฑฐํ•˜๋‹ค, ์„ธ๋‹ค ๋ผ๋Š” ์˜๋ฏธ๋ฅผ ๊ฐ€์ง€๋Š”๋ฐ์š”, Swift์—์„œ๋Š” ์–ด๋–ค ์˜๋ฏธ๋กœ ์‚ฌ์šฉ๋˜๋Š”์ง€ ๋จผ์ € ๊ณต์‹ ๋ฌธ์„œ๋ฅผ ํ†ตํ•ด enumerated()์˜ ์ •์˜๋ถ€ํ„ฐ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค!

 

(nx)๋กœ ์ด๋ฃจ์–ด์ง„ ์Œ์˜ ์‹œํ€€์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ n์€ 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋Š” ์—ฐ์†์ ์ธ ์ •์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ด๊ณ  x๋Š” ์‹œํ€€์Šค์˜ ์š”์†Œ๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.

์ •๋„๋กœ ํ•ด์„ํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์€๋ฐ์š”, ์š”์•ฝ์„ ํ•ด๋ณด์ž๋ฉด enumerated() ํ•จ์ˆ˜๋Š” return ํƒ€์ž…์ด ํŠœํ”Œ ํ˜•์‹์ธ ์ธ์Šคํ„ด์Šค ๋ฉ”์„œ๋“œ์ธ๋ฐ, ํŠœํ”Œ์—์„œ ์•ž์— ์œ„์น˜ํ•˜๋Š” ๊ฐ’์€ 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋Š” integer ๊ฐ’์ด๊ณ , ๋’ค์— ์œ„์น˜ํ•˜๋Š” ๊ฐ’์€ ์‹œํ€€์Šค์˜ ์š”์†Œ์ž…๋‹ˆ๋‹ค.

enumerated()์˜ ํ™œ์šฉ

๊ณต์‹ ๋ฌธ์„œ์— enumerated()๋ฅผ ํ™œ์šฉํ•œ ์˜ˆ์‹œ ์ฝ”๋“œ๋„ ๋‚˜์™€์žˆ์Šต๋‹ˆ๋‹ค!

 

for (n, c) in "Swift".enumerated() {
    print("\(n): '\(c)'")
}
// Prints "0: 'S'"
// Prints "1: 'w'"
// Prints "2: 'i'"
// Prints "3: 'f'"
// Prints "4: 't'"

 

์œ„์˜ ์˜ˆ์‹œ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด enumerated()๊ฐ€ ๋ฐ˜ํ™˜ํ•˜๋Š” ํŠœํ”Œ์— ์–ด๋–ค ๊ฐ’์„ ๋‹ด๊ณ  ์žˆ๋Š”์ง€ ์ง๊ด€์ ์œผ๋กœ ์ดํ•ดํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์€๋ฐ์š”,

๋ฐ˜๋ณต๋ฌธ ์•ˆ์—์„œ ํ”„๋ฆฐํŠธ๋ฌธ์„ ํ†ตํ•ด ํŠœํ”Œ์˜ ๊ฐ’์„ ์ฐจ๋ก€๋กœ ์ฐ์–ด๋ณด๋ฉด "Swift"๋ผ๋Š” ๋ฌธ์ž์—ด์ด ๊ฐ ๋ฌธ์ž์˜ ์œ„์น˜์™€ ํ•จ๊ป˜ ์ˆœ์„œ๋Œ€๋กœ ์—ด๊ฑฐ๋˜๊ณ  ์žˆ๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์ €๋Š” ๋ฌธ์ž์—ด์„ ๋‹ค๋ฃจ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ๋ฅผ ํ’€๋•Œ ๋ฌธ์ž์—ด์„ ์ด๋ฃจ๊ณ  ์žˆ๋Š” ๊ฐ ๋ฌธ์ž์˜ ์ธ๋ฑ์Šค ๊ฐ’์„ ๊ฐ€์ ธ์˜ค๊ธฐ ์œ„ํ•ด Array๋กœ ๋ณ€ํ™˜ํ•ด์„œ ๋งŽ์ด ์‚ฌ์šฉํ–ˆ์—ˆ๋Š”๋ฐ์š”, enumerated() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด Arrayํ˜•์œผ๋กœ ๋ณ€ํ™˜ํ•˜์ง€ ์•Š๊ณ ๋„ ์‰ฝ๊ฒŒ ๊ฐ ๋ฌธ์ž์˜ ์œ„์น˜๋ฅผ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!

์ปฌ๋ ‰์…˜ ํƒ€์ž…์—์„œ์˜ ์‚ฌ์šฉ

Array ์ด์•ผ๊ธฐ๊ฐ€ ๋‚˜์™”์œผ๋‹ˆ ์ปฌ๋ ‰์…˜ ํƒ€์ž…์—์„œ์˜ ์‚ฌ์šฉ๋„ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค!

Swift์˜ ์ปฌ๋ ‰์…˜ ํƒ€์ž…์—๋Š” Array, Dictionary, Set์ด ์žˆ์Šต๋‹ˆ๋‹ค.

Array

let names: Array = ["Sofia", "Camilla", "Martina", "Mateo", "Nicolás"]

for (n, x) in names.enumerated() {
    print(n, x)
}

// 0 Sofia
// 1 Camilla
// 2 Martina
// 3 Mateo
// 4 Nicolás

 

Array์— enumerated()๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฆฌํ„ด๋˜๋Š” n๊ฐ’์ด index์™€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค.

Dictionary์™€ Set

let names: Set = ["Sofia", "Camilla", "Martina", "Mateo", "Nicolás"]
let dic: [String: String] = ["a": "A", "b": "B", "c": "C"]

for (n, x) in names.enumerated() {
    print(n, x)
}

for (n, x) in dic.enumerated() {
    print(n, x.value)
}

// ์ˆœ์„œ๋Š” ๋นŒ๋“œํ•  ๋•Œ๋งˆ๋‹ค ๋ฐ”๋€œ
// 0 Mateo
// 1 Nicolás
// 2 Sofia
// 3 Martina
// 4 Camilla
// 0 C
// 1 A
// 2 B

 

Array์™€ ๋‹ฌ๋ฆฌ Dictionary์™€ Set์—๋Š” ์ •์˜๋œ ์ˆœ์„œ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.

๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— n์ด ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚˜๊ฑฐ๋‚˜ ์ธ๋ฑ์Šค๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋Š” ํƒ€์ž…์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

Dictionary์™€ Set์— enumerated()๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ์ธ๋ฑ์Šค๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค๋Š” ์ ์„ ์œ ์˜ํ•˜๋ฉฐ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

sorted() ํ•จ์ˆ˜๋ฅผ ํ™œ์šฉํ•ด ์–ด๋–ค ๊ธฐ์ค€์— ๋”ฐ๋ฅธ ์ˆœ์„œ๋ฅผ ๋ถ€์—ฌํ•œ ๋‹ค์Œ ์‚ฌ์šฉํ•ด๋„ ๋  ๊ฒƒ ๊ฐ™๋‹ค๋Š” ์ƒ๊ฐ๋„ ๋“œ๋„ค์š”!

 


[์ฐธ๊ณ  ์ž๋ฃŒ]

 

Apple Developer Documentation

 

developer.apple.com