Potato
์•ˆ๋…•ํ•˜์„ธ์š”, ๊ฐ์žก๋‹ˆ๋‹ค?๐Ÿฅ” ^___^ ๐Ÿ˜บ github ๋ฐ”๋กœ๊ฐ€๊ธฐ ๐Ÿ‘‰๐Ÿป

Swift/Swift Documents

[Swift] ๊ณต์‹ ๋ฌธ์„œ ์ •๋ฆฌ (4) - ์ปฌ๋ ‰์…˜ ํƒ€์ž… (Collection Types) - array(๋ฐฐ์—ด), dictionary(๋”•์…”๋„ˆ๋ฆฌ), set(์ง‘ํ•ฉ)

๊ฐ์ž ๐Ÿฅ” 2022. 1. 14. 15:50
๋ฐ˜์‘ํ˜•

์‹œ์ž‘ํ•˜๋ฉฐ

iOS ๊ฐœ๋ฐœ์„ ์œ„ํ•œ ๋‹ค๋ฅธ ์ธํ„ฐ๋„ท ๊ฐ•์˜๋ฅผ ๋“ค์œผ๋ฉฐ ๊ธฐ์ดˆ์ ์ธ ๋ฌธ๋ฒ•์„ ๋น ๋ฅด๊ฒŒ ์ตํžˆ๊ณ  ์žˆ๊ธด ํ•˜์ง€๋งŒ, swift์˜ ํŠน์ง•์„ ์˜จ์ „ํ•˜๊ฒŒ ์ž˜ ํŒŒ์•…ํ•˜๊ธฐ ์œ„ํ•ด์„œ ๊ณต์‹๋ฌธ์„œ๋ฅผ ๊ผญ ๊ณต๋ถ€ํ•˜๊ณ , ์ •๋ฆฌํ•ด๋ณด๋„๋ก ํ•˜์ž. ์˜ค๋Š˜์€ ๊ทธ 4๋ฒˆ์งธ ์ˆœ์„œ, Collection types ์ด๋‹ค.

Swift ๊ณต์‹ Documents (4) Collection Types

๋ฐ˜์‘ํ˜•
 

Collection Types — The Swift Programming Language (Swift 5.5)

Collection Types Swift provides three primary collection types, known as arrays, sets, and dictionaries, for storing collections of values. Arrays are ordered collections of values. Sets are unordered collections of unique values. Dictionaries are unordere

docs.swift.org


Collection Types (์ปฌ๋ ‰์…˜ ํƒ€์ž…)

Swift๋Š” ์ปฌ๋ ‰์…˜ ํƒ€์ž…์œผ๋กœ Array(๋ฐฐ์—ด), Set(์ง‘ํ•ฉ), Dictionary(๋”•์…”๋„ˆ๋ฆฌ) ์œ ํ˜•์„ ์ œ๊ณตํ•œ๋‹ค. ๋ฐฐ์—ด์€ ๊ฐ’์ด ์ˆœ์„œ๋Œ€๋กœ ์ •๋ ฌ๋œ ๋ชจ์Œ์ด๊ณ , ์ง‘ํ•ฉ์€ ๊ณ ์œ ์˜ ๊ฐ’๋“ค์„ (์ค‘๋ณตํ•˜์ง€ ์•Š๋‹ค๋Š” ์˜๋ฏธ) ์ •๋ ฌ๋˜์ง€ ์•Š๊ฒŒ (์ˆœ์„œ์—†์ด) ๋ชจ์•„๋†“์€ ๋ชจ์Œ์ด๊ณ , ๋”•์…”๋„ˆ๋ฆฌ๋Š” Key์™€ Value๋ฅผ ์—ฐ๊ฒฐํ•ด๋†“์€ ์ •๋ ฌ๋˜์ง€ ์•Š์€ ๋ชจ์Œ์ด๋‹ค. 

์ถœ์ฒ˜: docs.swift.org ์• ํ”Œ ์Šค์œ„ํ”„ํŠธ ๊ณต์‹ ๋ฌธ์„œ

Swift์˜ ๋ฐฐ์—ด, ๋”•์…”๋„ˆ๋ฆฌ, ์ง‘ํ•ฉ์— ์ €์žฅ๋˜๋Š” ๋ชจ๋“  ๊ฐ’๋“ค์€ ํ•ญ์ƒ ๋ช…ํ™•ํ•œ ํƒ€์ž…์„ ๊ฐ€์ง€๊ณ  ์žˆ์–ด์•ผ ํ•œ๋‹ค. ๋งŒ์•ฝ Int ์ž๋ฃŒํ˜•์„ ์ €์žฅํ•˜๋Š” Array(๋ฐฐ์—ด)์— String ํƒ€์ž…์„ ๊ฐ€์ง„ ๊ฐ’์„ ์ €์žฅํ•˜๋ ค๊ณ  ํ•˜๋ฉด, ์˜ค๋ฅ˜๋ฅผ ๋‚ธ๋‹ค.


Mutability of Collections

๊ฐœ๋ฐœ์ž๊ฐ€ ๋ฐฐ์—ด, ์ง‘ํ•ฉ, ์‚ฌ์ „์„ ๋งŒ๋“ค์–ด์„œ ๋ณ€์ˆ˜์— ํ• ๋‹นํ•˜๋ฉด ๊ทธ ์ปฌ๋ ‰์…˜์€ ๋ณ€๊ฒฝ ๊ฐ€๋Šฅํ•˜๋‹ค. ์ฆ‰, ์ปฌ๋ ‰์…˜์— ๊ฐ’์„ ์ถ”๊ฐ€ํ•˜๊ณ , ์ œ๊ฑฐ, ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ํ•˜์ง€๋งŒ ๊ฐœ๋ฐœ์ž๊ฐ€ ๋ฐฐ์—ด, ์ง‘ํ•ฉ, ์‚ฌ์ „์„ ์ƒ์ˆ˜๋กœ ์„ ์–ธ(let)ํ•˜๋ฉด, ๊ทธ ์ปฌ๋ ‰์…˜์€ ํฌ๊ธฐ์™€ ๊ฐ’ ์ถ”๊ฐ€, ๊ฐ’ ๋ณ€๊ฒฝ ๋“ฑ ์–ด๋– ํ•œ ๋ณ€๊ฒฝ๋„ ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค.


Arrays (๋ฐฐ์—ด)

๋ฐฐ์—ด์€ ๊ฐ™์€ type์˜ ๊ฐ’๋“ค์„ ์ •๋ ฌ๋œ ๋ฆฌ์ŠคํŠธ์— ์ €์žฅํ•œ๋‹ค. ๋™์ผํ•œ ๊ฐ’์ด ๋ฐฐ์—ด์— ์„œ๋กœ ๋‹ค๋ฅธ ์œ„์น˜์— ์—ฌ๋Ÿฌ๋ฒˆ ๋‚˜ํƒ€๋‚  ์ˆ˜ ์žˆ๋‹ค. 

Array Type Shorthand Syntax

Swift์—์„œ ๋ฐฐ์—ด์€ Array๋กœ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, Element์—๋Š” ์ €์žฅํ•  ๊ฐ’๋“ค์˜ ํƒ€์ž…์„ ์“ฐ๋ฉด ๋œ๋‹ค. [element]๋กœ ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

Creating an Empty Array

๊ฐœ๋ฐœ์ž๋Š” ์•„๋ž˜์™€ ๊ฐ™์€ ์ƒ์„ฑ์ž ๊ตฌ๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ ํŠน์ • ์œ ํ˜•์˜ ๋นˆ ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

var someInts: [Int] = []
print("someInts is of type [Int] with \(someInts.count) items.")
// Prints "someInts is of type [Int] with 0 items."

๋ฐฐ์—ด ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ• โ‘  โ–ฒโ–ฒโ–ฒ

somInts ๋ฐฐ์—ด์˜ ์œ ํ˜•์€ Int ๋กœ ์ง€์ •ํ•˜์—ฌ ์ƒ์„ฑํ–ˆ๋‹ค.

๋˜ํ•œ ๊ฐœ๋ฐœ์ž๋Š” [] ๋นˆ ๊ด„ํ˜ธ๋ฅผ ํ™œ์šฉํ•˜์—ฌ ์ด๋ฏธ ๊ฐ’์ด ๋“ค์–ด์žˆ๋Š” ๋ฐฐ์—ด์„ ๋นˆ ๋ฐฐ์—ด๋กœ ๋‹ค์‹œ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. ํ•ด๋‹น ๋ฐฐ์—ด์€ ์ดˆ๋ฐ˜์— ์ž‘์„ฑํ–ˆ๋˜ Int ํƒ€์ž…์„ ๊ฐ–๋Š” ๋ฐฐ์—ด์ด๋ผ๋Š” ์ ์€ ์œ ์ง€๋œ๋‹ค.

someInts.append(3)
// someInts now contains 1 value of type Int
someInts = []
// someInts is now an empty array, but is still of type [Int]

 

Creating an Array with a Default Value

Swift์˜ Array ์œ ํ˜•์€ ๋ชจ๋“ ๊ฐ’์ด ๋™์ผํ•œ ๊ฐ’์œผ๋กœ ์กด์žฌํ•˜๊ฒŒ ๋งŒ๋“œ๋Š” ์ƒ์„ฑ์ž๋ฅผ ์ œ๊ณตํ•œ๋‹ค. ์ƒ์„ฑ์ž์˜ repeating์—๋Š” ํ•œ ๊ฐ’์„ ๋™์ผํ•˜๊ฒŒ ๋„ฃ์„ ๊ฒƒ์ธ์ง€, count์—๋Š” ๋ช‡ ๊ฐœ์˜ ๊ฐ’์„ ์ƒ์„ฑํ•  ๊ฒƒ์ธ์ง€๋ฅผ ๋ช…์‹œํ•ด์ฃผ๋ฉด ๋ฐฐ์—ด์ด ์ƒ์„ฑ๋œ๋‹ค.

์•„๋ž˜ ์ฝ”๋“œ๋Š” threeDoubles ๋ฐฐ์—ด์„ ์ƒ์„œํ•˜๋Š” ์ฝ”๋“œ์ด๊ณ , ๋ชจ๋“  ๊ฐ’์ด ๋ถ€๋™ ์†Œ์ˆ˜์  ํ˜•ํƒœ, ๊ทธ๋ฆฌ๊ณ  ๋ฐฐ์—ด์˜ ๊ธธ์ด๋Š” 3์ด ๋œ๋‹ค.

var threeDoubles = Array(repeating: 0.0, count: 3)
// threeDoubles is of type [Double], and equals [0.0, 0.0, 0.0]

 

Creating an Array by Adding Two Arrays Together (๋ฐฐ์—ด ๋”ํ•˜๊ธฐ +)

์—ฐ์‚ฐ์ž + ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋‘ ๊ฐœ์˜ ๋ฐฐ์—ด์„ ํ•ฉ์ณ ์ƒˆ ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‘ ๊ฐœ์˜ Array๋Š” ๊ฐ™์€ ํƒ€์ž…์˜ Array์—ฌ์•ผ ํ•œ๋‹ค. 

var anotherThreeDoubles = Array(repeating: 2.5, count: 3)
// anotherThreeDoubles is of type [Double], and equals [2.5, 2.5, 2.5]

var sixDoubles = threeDoubles + anotherThreeDoubles
// sixDoubles is inferred as [Double], and equals [0.0, 0.0, 0.0, 2.5, 2.5, 2.5]

 

Creating an Array with an Array Literal (๋ฐฐ์—ด ๋ฆฌํ„ฐ๋Ÿด์„ ํ™œ์šฉํ•˜์—ฌ ๋ฐฐ์—ด ๋งŒ๋“ค๊ธฐ)

๋ฐฐ์—ด ๋ฆฌํ„ฐ๋Ÿด์„ ์‚ฌ์šฉํ•ด์„œ ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. ์ดˆ๊ธฐํ™” ์‹œํ‚ฌ ๊ฐ’๋“ค์˜ ํƒ€์ž…์„ ์ž˜ ์ค€์ˆ˜ํ•ด์•ผ ํ•œ๋‹ค. ์•„๋ž˜ ์ฝ”๋“œ๋Š” Array ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋˜๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์ด๋‹ค.

var shoppingList: [String] = ["Eggs", "Milk"]
// shoppingList has been initialized with two initial items

๋ฐฐ์—ด ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ• โ‘ก โ–ฒโ–ฒโ–ฒ

์ด๋ ‡๊ฒŒ ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•˜๊ธฐ๋„ ํ•˜์ง€๋งŒ, swift๋Š” ํƒ€์ž…์„ ์•ˆ์จ์ค˜๋„ ์ถ”๋ก ํ•ด์„œ ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ด๋Ÿฐ swift์˜ ๊ธฐ๋Šฅ ๋•๋ถ„์— ์šฐ๋ฆฌ๋Š” ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•  ๋•Œ ํƒ€์ž…์„ ๋”ฐ๋กœ ์ž‘์„ฑํ•˜์ง€ ์•Š๊ณ  ์•„๋ž˜์™€ ๊ฐ™์ด ๊ฐ„๋‹จํ•˜๊ฒŒ ์ฝ”๋“œ๋ฅผ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

var shoppingList = ["Eggs", "Milk"]
// ํƒ€์ž… ์ถ”๋ก  ๊ธฐ๋Šฅ์ด ์žˆ์–ด์„œ ์ž๋™์œผ๋กœ ํ•ด๋‹น ๋ฐฐ์—ด์€ String ํƒ€์ž…์ด๋ผ๊ณ  ์ธ์‹ํ•œ๋‹ค.

๋ฐฐ์—ด ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ• โ‘ข โ–ฒโ–ฒโ–ฒ

 

Accessing and Modifying an Array (๋ฐฐ์—ด์— ์ ‘๊ทผํ•˜๊ณ  ์ˆ˜์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•)

Array์˜ ๋ฉ”์„œ๋“œ์™€ ํ”„๋กœํผํ‹ฐ๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๋ฐฐ์—ด์— ์ ‘๊ทผํ•˜๊ฑฐ๋‚˜ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. 

โ–ท Count
์ด์ฒ˜๋Ÿผ array.count ๋ฅผ ํ™œ์šฉํ•˜๋ฉด ํ•ด๋‹น ๋ฐฐ์—ด์˜ ๊ธธ์ด๊ฐ€ ์ˆซ์ž ํ˜•ํƒœ๋กœ ์ถœ๋ ฅ๋œ๋‹ค.

print("The shopping list contains \(shoppingList.count) items.")
// Prints "The shopping list contains 2 items."

 

โ–ท isEmpty
Boolean isEmpty ํ”„๋กœํผํ‹ฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์นด์šดํŠธ ์†์„ฑ์ด 0 ์ธ์ง€ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

if shoppingList.isEmpty {
    print("The shopping list is empty.")
} else {
    print("The shopping list isn't empty.")
}
// Prints "The shopping list isn't empty."

 

โ–ท append
append(_:) ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ์ƒˆ๋กœ์šด ๊ฐ’์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค.

shoppingList.append("Flour")
// shoppingList now contains 3 items, and someone is making pancakes

 

โ–ท ์—ฐ์‚ฐ์ž +=๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐฐ์—ด์— ๊ฐ’ ์ถ”๊ฐ€ํ•˜๊ธฐ
์•ž์—์„œ ๋ฐฐ์—ด๋ผ๋ฆฌ + ์—ฐ์‚ฐ์ž๋„ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค๊ณ  ํ–ˆ๋Š”๋ฐ, += ์—ฐ์‚ฐ์ž ๋˜ํ•œ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค. 

shoppingList += ["Baking Powder"]
// shoppingList now contains 4 items
shoppingList += ["Chocolate Spread", "Cheese", "Butter"]
// shoppingList now contains 7 items

 

โ–ท ์ธ๋ฑ์Šค ํ™œ์šฉํ•˜์—ฌ ๋ฐฐ์—ด์— ์ ‘๊ทผํ•˜๊ธฐ
index๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๋ฐฐ์—ด์— ์ ‘๊ทผ ๊ฐ€๋Šฅํ•˜๋‹ค. 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•œ๋‹ค.

var firstItem = shoppingList[0]
// firstItem is equal to "Eggs"

 

โ–ท ์ธ๋ฑ์Šค ํ™œ์šฉํ•˜์—ฌ ๋ฐฐ์—ด ๊ฐ’ ๋ฐ”๊พธ๊ธฐ
index๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๋ฐฐ์—ด์— ์ ‘๊ทผํ•˜๊ณ , ํ•ด๋‹น ์œ„์น˜์— ์žˆ๋Š” ๊ฐ’์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค.

shoppingList[0] = "Six eggs"
// the first item in the list is now equal to "Six eggs" rather than "Eggs"

shoppingList[4...6] = ["Bananas", "Apples"]
// shoppingList now contains 6 items
// 4,5,6 ๋ฒˆ์ž๋ฆฌ์— ์žˆ๋Š” ๊ฐ’์„ ๋นผ๊ณ  ์ƒˆ๋กœ์šด ๋‘ ๊ฐœ์˜ ๊ฐ’์„ ๋„ฃ์—ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ ๊ธธ์ด๋„ ์ค„์–ด๋“ ๋‹ค.

 

โ–ท insert ํ™œ์šฉํ•˜์—ฌ ํŠน์ • ์œ„์น˜์— ๊ฐ’ ์ถ”๊ฐ€ํ•˜๊ธฐ
insert(_:at:) ์„ ํ™œ์šฉํ•˜๋ฉด ๋ฐฐ์—ด์˜ ํŠน์ •์œ„์น˜์— ์ƒˆ๋กœ์šด ๊ฐ’์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ฝ”๋“œ๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

shoppingList.insert("Maple Syrup", at: 0)
// shoppingList now contains 7 items
// "Maple Syrup" is now the first item in the list

 

โ–ท remove ํ™œ์šฉํ•˜์—ฌ ํŠน์ • ์œ„์น˜์˜ ๊ฐ’ ์ œ๊ฑฐํ•˜๊ธฐ (remove, removeLast)
insert(_:at:) ์„ ํ™œ์šฉํ•˜๋ฉด ๋ฐฐ์—ด์˜ ํŠน์ •์œ„์น˜์— ์ƒˆ๋กœ์šด ๊ฐ’์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ฝ”๋“œ๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

let mapleSyrup = shoppingList.remove(at: 0)
// the item that was at index 0 has just been removed
// shoppingList now contains 6 items, and no Maple Syrup
// the mapleSyrup constant is now equal to the removed "Maple Syrup" string

// โ˜…โ˜… ๋ฐฐ์—ด์˜ ๋งจ ๋งˆ์ง€๋ง‰ ๊ฐ’ ์‚ญ์ œํ•  ๋•Œ removeLast
let apples = shoppingList.removeLast()
// the last item in the array has just been removed
// shoppingList now contains 5 items, and no apples
// the apples constant is now equal to the removed "Apples" string

๋งŒ์•ฝ insert ๋‚˜ remove ์ฒ˜๋Ÿผ ํŠน์ • ์ธ๋ฑ์Šค๋ฅผ ์ง€์ •ํ•ด์„œ ์ˆ˜์ •ํ•˜๋Š” ํ”„๋กœํผํ‹ฐ์˜ ๊ฒฝ์šฐ ๋ฐฐ์—ด์˜ ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚˜๋Š” ์ธ๋ฑ์Šค๋ฅผ ์ง€์ •ํ•˜๋ฉด ๋‹น์—ฐํžˆ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

 

Iterating Over an Array (for in ๊ตฌ๋ฌธ, enumerated)

for-in ๊ตฌ๋ฌธ์„ ํ™œ์šฉํ•˜์—ฌ ๋ฐฐ์—ด์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค.

for item in shoppingList {
    print(item)
}
// Six eggs
// Milk
// Flour
// Baking Powder
// Bananas

๋งŒ์•ฝ Array์˜ index๊ฐ€ ํ•„์š”ํ•˜๋‹ค๋ฉด, enumerated() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด, index์™€ ๊ฐ’์„ ํ•จ๊ป˜ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

for (index, value) in shoppingList.enumerated() {
    print("Item \(index + 1): \(value)")
}
// Item 1: Six eggs
// Item 2: Milk
// Item 3: Flour
// Item 4: Baking Powder
// Item 5: Bananas

Sets (์ง‘ํ•ฉ)

Set์€ ๋™์ผํ•œ ํƒ€์ž…์˜ ๊ฐ’์ด ์ˆœ์„œ ์—†์ด ์ €์žฅ๋œ ์ปฌ๋ ‰์…˜ ํƒ€์ž…์ด๋‹ค. Set์€ ์ค‘๋ณต ๊ฐ’์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์ด๋Ÿฌํ•œ ํŠน์ง•์„ ์‚ฌ์šฉํ•˜๋Š” ๊ณณ์—์„œ๋Š” array๋Œ€์‹  set์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.

Hash Values for Set Types (์ง‘ํ•ฉ์˜ ํ•ด์‹œ)

Set์— ์ €์žฅ๋˜๊ธฐ ์œ„ํ•ด์„œ๋Š” hash๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ์–ด์•ผ ํ•œ๋‹ค. ์—ฌ๊ธฐ์„œ ์‚ฌ์šฉ๋˜๋Š” ํ•ด์‹œ๊ฐ’์€ intํ˜•์ด๋‹ค. ์ฆ‰ A์™€ B๊ฐ€ ์žˆ์„ ๋•Œ, A์˜ ํ•ด์‹œ ๊ฐ’๊ณผ B์˜ ํ•ด์‹œ๊ฐ’์ด ๊ฐ™๋‹ค๋ฉด A์™€ B๋Š” ๊ฐ™์€ ๊ฐ’์ด๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ๋ชจ๋“  Swift์˜ ๊ธฐ๋ณธ์ ์ธ ํƒ€์ž… (Int, String, Double, Float, Bool)์€ ํ•ด์‹œ๊ฐ’์œผ๋กœ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๊ณ , ํ•ด์‹œ๊ฐ’์€ set์˜ ๊ฐ’์ด๋‚˜ Dictionary์˜ key๊ฐ’์ด ๋  ์ˆ˜ ์žˆ๋‹ค. 

Set type Syntax

set์€ set์œผ๋กœ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๊ณ , element๋Š” set์— ์ €์žฅ๋  ์ˆ˜ ์žˆ๋Š” ๊ฐ’๋“ค์˜ ํƒ€์ž…์ด๋‹ค.

 

Creating and Initializing an Empty Set

๊ฐœ๋ฐœ์ž๋Š” ์ƒ์„ฑ์ž ๊ตฌ๋ฌธ์„ ํ™œ์šฉํ•˜์—ฌ ๋นˆ ์ง‘ํ•ฉ์„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

var letters = Set<Character>()
print("letters is of type Set<Character> with \(letters.count) items.")
// Prints "letters is of type Set<Character> with 0 items."

๋˜ํ•œ [] ๋นˆ ๊ด„ํ˜ธ๋ฅผ ํ™œ์šฉํ•˜์—ฌ set์„ ์ดˆ๊ธฐํ™”ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ์ด๋ฏธ ์ดˆ๊ธฐ์— ์ƒ์„ฑ๋œ ํƒ€์ž…์€ ๋ฐ”๋€Œ์ง€ ์•Š๋Š”๋‹ค. 

letters.insert("a")
// letters now contains 1 value of type Character
letters = []
// letters is now an empty set, but is still of type Set<Character>

 

Creating a Set with an Array Literal

Set์„ ์ฒ˜์Œ ๋งŒ๋“ค ๋•Œ ํ•˜๋‚˜ ์ด์ƒ์˜ ๋ฐฐ์—ด ๋ฆฌํ„ฐ๋Ÿด์„ ํ™œ์šฉํ•˜์—ฌ ์ง‘ํ•ฉ์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

var favoriteGenres: Set<String> = ["Rock", "Classical", "Hip hop"]
// favoriteGenres has been initialized with three initial items

์ง‘ํ•ฉ์„ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ• โ–ฒโ–ฒโ–ฒ

์ง‘ํ•ฉ์„ ์ƒ์„ฑํ•  ๋•Œ๋„ ๋ณ€์ˆ˜(var), ์ƒ์ˆ˜(let)์œผ๋กœ ์„ ์–ธํ•˜๋Š๋ƒ์— ๋”ฐ๋ผ ๊ฐ’์˜ ์ถ”๊ฐ€ ๋ฐ ๋ณ€ํ˜•์ด ๊ฐ€๋Šฅํ•œ ์œ ๋ฌด๊ฐ€ ๋‹ฌ๋ผ์ง„๋‹ค. Set๋„ ์ €์žฅ๋˜๋Š” ์ž๋ฃŒํ˜•์„ ๊ผญ ์จ์ฃผ์ง€ ์•Š์•„๋„ Swift๋Š” ์ž๋™์œผ๋กœ ํƒ€์ž…์„ ์œ ์ถ”ํ•˜๊ณ  ์ถ”๋ก ํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ์•„๋ž˜์™€ ๊ฐ™์ด ํƒ€์ž…๋ช…์„ ์‚ญ์ œํ•˜๊ณ  Set์„ ์ƒ์„ฑํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

var favoriteGenres: Set = ["Rock", "Classical", "Hip hop"]

 

Accessing and Modifying a Set

Set์—์„œ๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋‹ค์–‘ํ•œ ์ƒ์„ฑ์ž์™€ ํ”„๋กœํผํ‹ฐ๊ฐ€ ์กด์žฌํ•œ๋‹ค. ์ฐจ๋ก€๋Œ€๋กœ ์‚ดํŽด๋ณด๊ฒ ๋‹ค.

โ–ท count
Set์•ˆ์— ๋ช‡ ๊ฐœ์˜ ๊ฐ’์ด ์žˆ๋Š”์ง€ ๋ณผ ์ˆ˜ ์žˆ๋Š” ํ”„๋กœํผํ‹ฐ์ด๋‹ค.

print("I have \(favoriteGenres.count) favorite music genres.")
// Prints "I have 3 favorite music genres."

 

โ–ท isEmpty
isEmpty๋ฅผ ํ™œ์šฉํ•˜์—ฌ set ์•ˆ์— ๊ฐ’์ด 0๊ฐœ ์ธ์ง€ ์•„๋‹Œ์ง€ ํ™•์ธ ๊ฐ€๋Šฅํ•˜๋‹ค.

if favoriteGenres.isEmpty {
    print("As far as music goes, I'm not picky.")
} else {
    print("I have particular music preferences.")
}
// Prints "I have particular music preferences."

 

โ–ท insert(_:) 
insert ๋ฉ”์„œ๋“œ๋ฅผ ํ™œ์šฉํ•˜์—ฌ set์•ˆ์— ๊ฐ’์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค.
(๋ฐฐ์—ด์—์„œ๋Š” at์„ ํ™œ์šฉํ•˜์—ฌ ์–ด๋Š ์ธ๋ฑ์Šค ์œ„์น˜์— ๊ฐ’์„ ์ถ”๊ฐ€ํ•  ๊ฒƒ์ธ์ง€ ์ž‘์„ฑํ•˜์ง€๋งŒ, ์ง‘ํ•ฉ์€ ์ˆœ์„œ๊ฐ€ ์˜๋ฏธ๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์— ๊ทธ๋ƒฅ at์„ ์ง€์ •ํ•˜์ง€ ์•Š๊ณ  insertํ•œ๋‹ค.)

favoriteGenres.insert("Jazz")
// favoriteGenres now contains 4 items

 

โ–ท remove(_:) 
remove ๋ฉ”์„œ๋“œ๋ฅผ ํ™œ์šฉํ•˜์—ฌ set์•ˆ์— ๊ฐ’์„ ์ œ๊ฑฐํ•  ์ˆ˜ ์žˆ๋‹ค. removeAll ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋ฉด, ์ง‘ํ•ฉ ๋‚ด์˜ ๋ชจ๋“  ๊ฐ’์„ ์ œ๊ฑฐํ•  ์ˆ˜ ์žˆ๋‹ค. removeํ•จ์ˆ˜๋Š” ์‚ญ์ œํ•˜๋Š” ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ด์ฃผ๋Š” ๊ธฐ๋Šฅ์ด ์žˆ๋‹ค.

if let removedGenre = favoriteGenres.remove("Rock") {
    print("\(removedGenre)? I'm over it.")
} else {
    print("I never much cared for that.")
}
// Prints "Rock? I'm over it."

 

โ–ท contains(_:) 
contains ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ set์ด๋‚ด์— ์–ด๋–ค ๊ฐ’์ด ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธ ๊ฐ€๋Šฅ ํ•˜๋‹ค.

if favoriteGenres.contains("Funk") {
    print("I get up on the good foot.")
} else {
    print("It's too funky in here.")
}
// Prints "It's too funky in here."

 

Iterating Over a Set

array์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ set๋„ for in ๋ฃจํ”„๋กœ ๋ฐ˜๋ณตํ•ด์„œ ๋ชจ๋“  ๊ฐ’์„ ์ถœ๋ ฅ ๊ฐ€๋Šฅํ•˜๋‹ค.

for genre in favoriteGenres {
    print("\(genre)")
}
// Classical
// Jazz
// Hip hop

set์€ ์ˆœ์„œ๊ฐ€ ์ •์˜๋˜์–ด ์žˆ์ง€ ์•Š์ง€๋งŒ, sorted() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด, ์—ฐ์‚ฐ์ž์— ์˜ํ•œ ๋Œ€์†Œ ๋น„๊ต๋กœ ๊ฐ’๋“ค์„ ์ •๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค.

for genre in favoriteGenres.sorted() {
    print("\(genre)")
}
// Classical
// Hip hop
// Jazz

Performing Set Operations

Set์€ ์ง‘ํ•ฉ์ด๋ฏ€๋กœ, ์ฐจ์ง‘ํ•ฉ, ๊ต์ง‘ํ•ฉ, ํ•ฉ์ง‘ํ•ฉ ๋“ฑ์˜ ๊ฐœ๋…์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

Fundamental Set Operations (๊ธฐ๋ณธ ์ง‘ํ•ฉ ์—ฐ์‚ฐ์ž)

์•„๋ž˜ ๊ทธ๋ฆผ์—๋Š” ์Œ์˜ ์˜์—ญ์œผ๋กœ ํ‘œํ˜„๋˜๋Š” ๋‹ค์–‘ํ•œ ์ง‘ํ•ฉ ์—ฐ์‚ฐ์ž์˜ ๊ฒฐ๊ณผ๋ฅผ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

์ถœ์ฒ˜: docs.swift.org - ์Šค์œ„ํ”„ํŠธ ๊ณต์‹ ๋ฌธ์„œ collection types ๋‹จ์›

  • intersection(_:) (๊ต์ง‘ํ•ฉ) : ๋‘ ์ง‘ํ•ฉ์˜ ๊ณตํ†ต์ ์ธ ๊ฐ’๋งŒ ์‚ฌ์šฉํ•˜์—ฌ ์ƒˆ ์ง‘ํ•ฉ์„ ์ƒ์„ฑํ•œ๋‹ค.
  • symmetricDifference(_:) : ๋‘ ์ง‘ํ•ฉ์— ๊ต์ง‘ํ•ฉ์„ ์ œ์™ธํ•œ ๋‚˜๋จธ์ง€ ๊ฐ’๋“ค์„ ํ™œ์šฉํ•˜์—ฌ ์ง‘ํ•ฉ์„ ์ƒ์„ฑํ•œ๋‹ค.
  • union(_:) (ํ•ฉ์ง‘ํ•ฉ) : ๋‘ ์ง‘ํ•ฉ์˜ ๋ชจ๋“  ๊ฐ’์„ ํ™œ์šฉํ•˜์—ฌ ์ƒˆ ์ง‘ํ•ฉ์„ ์ƒ์„ฑํ•œ๋‹ค.
  • substracting(_:) (์ฐจ์ง‘ํ•ฉ) : ์ฐจ์ง‘ํ•ฉ์œผ๋กœ ์ง€์ •๋œ ์ง‘ํ•ฉ์—๋Š” ์—†๋Š” ๊ฐ’๋“ค์„ ์ œ๊ฑฐํ•œ ์ƒˆ๋กœ์šด ์ง‘ํ•ฉ์„ ์ƒ์„ฑํ•œ๋‹ค.
let oddDigits: Set = [1, 3, 5, 7, 9]
let evenDigits: Set = [0, 2, 4, 6, 8]
let singleDigitPrimeNumbers: Set = [2, 3, 5, 7]

oddDigits.union(evenDigits).sorted()
// [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
oddDigits.intersection(evenDigits).sorted()
// []
oddDigits.subtracting(singleDigitPrimeNumbers).sorted()
// [1, 9]
oddDigits.symmetricDifference(singleDigitPrimeNumbers).sorted()
// [1, 2, 9]

 

Set Membership and Equality

์•„๋ž˜์˜ ๊ทธ๋ฆผ์€ ์„ธ ์ง‘ํ•ฉ(a, b, c)์„ ๋‚˜ํƒ€๋‚ด๋ฉฐ, ์ง‘ํ•ฉ๋“ค ๊ฐ„์— ๊ณต์œ ๋˜๋Š” ์š”์†Œ๋“ค์„ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฒน์น˜๋Š” ์˜์—ญ๋“ค์„ ๋ณด์—ฌ์ค€๋‹ค. ์ง‘ํ•ฉ a๋Š” b์˜ ๋ชจ๋“  ์š”์†Œ๋ฅผ ํฌํ•จํ•˜๋ฏ€๋กœ ์ง‘ํ•ฉ a๋Š” ์ง‘ํ•ฉ b์˜ ์ƒ์œ„ ์ง‘ํ•ฉ์ด๋‹ค. ๋ฐ˜๋Œ€๋กœ, ์ง‘ํ•ฉ b๋Š” ์ง‘ํ•ฉ a์˜ ๋ถ€๋ถ„์ง‘ํ•ฉ์ด๋‹ค. ์™œ๋ƒํ•˜๋ฉด b์˜ ๋ชจ๋“  ์›์†Œ๋Š” ๋˜ํ•œ a์— ์˜ํ•ด ํฌํ•จ๋˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์ง‘ํ•ฉ b์™€ ์ง‘ํ•ฉ c๋Š” ๊ณตํ†ต์ ์ธ ์š”์†Œ๋ฅผ ๊ณต์œ ํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์„œ๋กœ ๋ถ„๋ฆฌ๋œ๋‹ค.

์ถœ์ฒ˜: docs.swift.org - ์Šค์œ„ํ”„ํŠธ ๊ณต์‹ ๋ฌธ์„œ collection types ๋‹จ์›

  • equal ์—ฐ์‚ฐ์ž (==) : ํ•ด๋‹น ์—ฐ์‚ฐ์ž๋ฅผ true๋กœ ๋ฐ˜ํ™˜์‹œํ‚ค๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋น„๊ต๋˜๋Š” ๋‘ set์˜ ๋ชจ๋“  ์›์†Œ๊ฐ€ ๊ฐ™์•„์•ผ ํ•œ๋‹ค.
  • isSubset(of:) ๋ฉ”์„œ๋“œ : ํ•ด๋‹น ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์–ด๋–ค set์˜ ๋ชจ๋“  ์›์†Œ๋ฅผ ํŠน์ • set์ด ๊ฐ–๊ณ  ์žˆ๋Š”์ง€ ํ™•์ธํ•ด์ค€๋‹ค.
  • isSuperset(of:) ๋ฉ”์„œ๋“œ : ๋ฐ˜๋Œ€๋กœ ์–ด๋– ํ•œ set์ด ํŠน์ • set์˜ ์›์†Œ๋ฅผ ๋ชจ๋‘ ๊ฐ€์ง€๊ณ  ์žˆ๋Š”์ง€ ํ™•์ธํ•ด์ค€๋‹ค.
  • isStrictSubset(of:) ํ˜น์€ isStrictSuperset(of:) : ๋งŒ์•ฝ ์–ด๋–ค set์ด ํŠน์ • set์˜ subset์ธ์ง€, superset์ธ๋ฉด์„œ ๊ฐ™์€ set์€ ์•„๋‹Œ์ง€๋ฅผ ํ™•์ธํ•ด์ค€๋‹ค.
  • isDisjoint(with:) : ๋‘ set์ด ๊ณตํ†ต์ ์œผ๋กœ ๊ฐ€์ง„ ์›์†Œ๊ฐ€ ํ•˜๋‚˜๋„ ์—†๋Š” ๊ฒƒ์„ ํ™•์ธํ•ด์ค€๋‹ค. 
let houseAnimals: Set = ["๐Ÿถ", "๐Ÿฑ"]
let farmAnimals: Set = ["๐Ÿฎ", "๐Ÿ”", "๐Ÿ‘", "๐Ÿถ", "๐Ÿฑ"]
let cityAnimals: Set = ["๐Ÿฆ", "๐Ÿญ"]

houseAnimals.isSubset(of: farmAnimals)
// true
farmAnimals.isSuperset(of: houseAnimals)
// true
farmAnimals.isDisjoint(with: cityAnimals)
// true

์ถ”๊ฐ€์„ค๋ช…) superset๊ณผ subset์˜ ๊ฐœ๋…์€ ๋ถ€๋ชจ์™€ ์ž์‹ ๊ด€๊ณ„๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด๋œ๋‹ค. farAnimals๋Š” houseAnimals์˜ ์›์†Œ๋ฅผ ๋ชจ๋‘ ํฌํ•จํ•˜๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— farmAnimals๊ฐ€ houseAnimals์˜ ๋ถ€๋ชจ(superset)๊ฐ€ ๋œ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค. 


Dictionaries(๋”•์…”๋„ˆ๋ฆฌ)

Dictionary๋Š” ket๊ฐ’๊ณผ value๊ฐ’์˜ ์Œ์ด ์ˆœ์„œ ์—†์ด ์ด๋ฃจ์–ด์ง„ ์ปฌ๋ ‰์…˜ ํƒ€์ž…์ด๋‹ค. ๋ชจ๋“  ๊ฐ’๋“ค์€ ์œ ์ผํ•œ ํ‚ค๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์žˆ์–ด์•ผ ํ•œ๋‹ค.

Dictionary type Shorthand Syntax

Dictionary๋Š” Dictinary<Key, Value> ๋กœ ํ‘œํ˜„ํ•œ๋‹ค. key์—๋Š” key๊ฐ’์˜ ํƒ€์ž…, value์—๋Š” value ๊ฐ’์˜ ํƒ€์ž…์„ ์จ ์ฃผ๋ฉด ๋œ๋‹ค. dictionary๋ฅผ [Key: Value] ๋กœ ํ‘œํ˜„ํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ๋‘ ๊ฐœ์˜ ํ‘œํ˜„๋ฐฉ์‹์ด ์žˆ์ง€๋งŒ ๊ฐ™์€ ์˜๋ฏธ๋ฅผ ๊ฐ–๋Š”๋‹ค.

 

Creating an Empty Dictionary (๋”•์…”๋„ˆ๋ฆฌ ์ƒ์„ฑํ•˜๊ธฐ!)

๋นˆ dictionary๋ฅผ ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ƒ์„ฑ์ž๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

var namesOfIntegers: [Int: String] = [:]
// namesOfIntegers is an empty [Int: String] dictionary

๋”•์…”๋„ˆ๋ฆฌ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ• โ‘  โ–ฒโ–ฒโ–ฒ

dictionary๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ํ•œ ๋ฒˆ ์„ ์–ธ ๋˜์—ˆ์œผ๋ฉด, ๋น„์–ด์žˆ๋Š” dictionary๋กœ ์ดˆ๊ธฐํ™” ๋˜์—ˆ๋‹ค๊ณ  ํ•ด๋„ ์ดˆ๊ธฐ์— ์„ค์ •ํ•œ ๊ทธ ํƒ€์ž…์€ ๋ฐ”๋€Œ์ง€ ์•Š๊ณ  ์œ ์ง€๋œ๋‹ค. [:] ๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๋นˆ ๋”•์…”๋„ˆ๋ฆฌ๋กœ ์ดˆ๊ธฐํ™” ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค.

์ฐธ๊ณ ) ๊ฐ’์„ ์ถ”๊ฐ€ํ•  ๋•Œ๋Š” dicName[Key] = 'Value' ๋กœ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค.

namesOfIntegers[16] = "sixteen"
// namesOfIntegers now contains 1 key-value pair
namesOfIntegers = [:]
// namesOfIntegers is once again an empty dictionary of type [Int: String]

 

Creating a Dictionary with a Dictionary Literal

dictionary literal๋กœ๋„ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. key:value ํ˜•์‹์œผ๋กœ ํ•˜๋‚˜์˜ key-value๋ฅผ ์ƒ์„ฑํ•˜๊ณ , ์ฝค๋งˆ(,)๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ ์ƒ์„ฑํ•˜๋ฉด ๋œ๋‹ค. ์•„๋ž˜์™€ ๊ฐ™์ด ํ•˜๋ฉด ๋‘ ๊ฐœ์˜ key-value์Œ์„ ๊ฐ€์ง„ ๋”•์…”๋„ˆ๋ฆฌ๊ฐ€ ์ƒ์„ฑ๋œ๋‹ค. ๋”•์…”๋„ˆ๋ฆฌ๋„ ์•ž์—์„œ ๋ฐฐ์šด ๊ฒƒ๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ํƒ€์ž… ์œ ํ˜•์„ ์ž‘์„ฑํ•˜์ง€ ์•Š์•„๋„ swift๋Š” ์ž๋™์œผ๋กœ ์œ ์ถ”, ์ถ”๋ก ํ•˜์—ฌ ํƒ€์ž…์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

// ํƒ€์ž… ์ ์–ด์„œ ๋”•์…”๋„ˆ๋ฆฌ ์ƒ์„ฑํ•˜๊ธฐ
var airports: [String: String] = ["YYZ": "Toronto Pearson", "DUB": "Dublin"]

//ํƒ€์ž… ์—†์ด ๊ฐ„๋‹จํ•˜๊ฒŒ ๋”•์…”๋„ˆ๋ฆฌ ์ƒ์„ฑํ•˜๊ธฐ
var airports = ["YYZ": "Toronto Pearson", "DUB": "Dublin"]

 

Accessing and Modifying a Dictionary (๋”•์…”๋„ˆ๋ฆฌ์— ์ ‘๊ทผํ•˜๊ณ  ์ˆ˜์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•)

๋”•์…”๋„ˆ๋ฆฌ๋„ ์•ž์—์„œ ๋ฐฐ์šด ๋ฐฐ์—ด, ์ง‘ํ•ฉ๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋‹ค์–‘ํ•œ ํ”„๋กœํผํ‹ฐ์™€ ๋ฉ”์„œ๋“œ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. ํ•ด๋‹น ๋ฉ”์„œ๋“œ, ํ”„๋กœํผํ‹ฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋”•์…”๋„ˆ๋ฆฌ์— ์ ‘๊ทผํ•˜๊ณ  ์ˆ˜์ • ๊ฐ€๋Šฅํ•˜๋‹ค. 

โ–ท count
count ๋ฉ”์„œ๋“œ๋Š” dictionary๊ฐ€ ๊ฐ–๊ณ  ์žˆ๋Š” key-value ์Œ์˜ ๊ฐœ์ˆ˜๋ฅผ ๋ณด์—ฌ์ค€๋‹ค.

print("The airports dictionary contains \(airports.count) items.")
// Prints "The airports dictionary contains 2 items."

 

โ–ท isEmpty
isEmpty ํ”„๋กœํผํ‹ฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋”•์…”๋„ˆ๋ฆฌ๊ฐ€ ๋น„์–ด์žˆ๋Š”์ง€ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ๋‹ค.

if airports.isEmpty {
    print("The airports dictionary is empty.")
} else {
    print("The airports dictionary isn't empty.")
}
// Prints "The airports dictionary isn't empty."

 

โ–ท ๋”•์…”๋„ˆ๋ฆฌ์— ๊ฐ’ ์ถ”๊ฐ€ํ•˜๊ธฐ
์•„๋ž˜์™€ ๊ฐ™์€ ๋ฐฉ๋ฒ•์œผ๋กœ ๋”•์…”๋„ˆ๋ฆฌ์— ๊ฐ’์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค.

airports["LHR"] = "London"
// the airports dictionary now contains 3 items

 

โ–ท ๋”•์…”๋„ˆ๋ฆฌ์— value๊ฐ’ ๋ณ€๊ฒฝํ•˜๊ธฐ (๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•~)
๋”•์…”๋„ˆ๋ฆฌ์— ๊ฐ’์„ ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐฉ์‹๊ณผ ๋™์ผํ•œ ๋ฐฉ์‹์œผ๋กœ ๊ฐ’์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค. key๊ฐ’์€ ์ค‘๋ณต๋˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์ƒˆ๋กœ์šด key๊ฐ’์„ ๊ฐ€์งˆ ๊ฒฝ์šฐ ์ถ”๊ฐ€๋˜๋Š” ๊ฒƒ์ด๊ณ , ๊ธฐ์กด์˜ key๊ฐ’์„ ๋ถ€๋ฅด๊ฒŒ ๋˜๋ฉด ํ•ด๋‹น key๊ฐ’์˜ value๋ฅผ ์ˆ˜์ •ํ•˜๊ฒŒ ๋œ๋‹ค.

airports["LHR"] = "London Heathrow"
// the value for "LHR" has been changed to "London Heathrow"

ํ•ด๋‹น ๋ฐฉ๋ฒ• ๋ง๊ณ  updateValue(_:forKey:) ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ’์„ ์ˆ˜์ •ํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ๋งŒ์•ฝ ์ด ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ์—†๋Š” key์— ๋Œ€ํ•ด์„œ ์ˆ˜์ •ํ•˜๋ ค๊ณ  ํ•˜๋ฉด, ์ˆ˜์ •์ด ์•„๋‹Œ ์ƒ์„ฑ์„ ํ•˜๊ฒŒ ๋œ๋‹ค. (์œ„์™€ ๋™์ผ!)

updateValue() ๋ฉ”์„œ๋“œ๋Š” ๊ธฐ์กด์˜ value๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ด์ค€๋‹ค. ์ด๋•Œ ๋ฐ˜ํ™˜๋˜๋Š” ๊ฐ’์€ ์˜ต์…”๋„ ๊ฐ’์œผ๋กœ ๋ฐ˜ํ™˜๋œ๋‹ค. ์ฆ‰, ๋งŒ์•ฝ String ๊ฐ’์„ ์ €์žฅํ•ด๋’€๋‹ค๋ฉด, Sprint? ํ˜น์€ optional(String)๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ๋งŒ์•ฝ ์—†๋Š” key๊ฐ’์— value๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๋ ค๊ณ  ํ–ˆ๋‹ค๋ฉด nil๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๊ฒŒ ๋œ๋‹ค.

if let oldValue = airports.updateValue("Dublin Airport", forKey: "DUB") {
    print("The old value for DUB was \(oldValue).")
}
// Prints "The old value for DUB was Dublin."

Dictionary์—์„œ ํŠน์ •ํ•œ key๊ฐ’์— ์ ‘๊ทผํ•  ๋•Œ key๊ฐ’์ด ์กด์žฌํ•˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์˜ต์…”๋„ ๊ฐ’์œผ๋กœ ๊ฒฐ๊ณผ๋ฅผ ์ค€๋‹ค. (์˜ต์…”๋„ ๊ฐ’๋งŒ์ด nil์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค.) ์ฆ‰ ์กด์žฌํ•˜์ง€ ์•Š๋Š” key๊ฐ’์— ์ ‘๊ทผํ•˜๋ฉด nil ์„ ๋ฐ˜ํ™˜์‹œ์ผœ์ค€๋‹ค. ์ด๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ์˜ต์…”๋„ ๋ฐ”์ธ๋”ฉ์„ ํ™œ์šฉํ•ด์„œ value ๊ฐ’์„ ์ถ”์ถœํ•ด์ค˜์•ผ ํ•œ๋‹ค.  (์ฐธ๊ณ : Swift ๊ณต์‹ ๋ฌธ์„œ ์ •๋ฆฌ (1) - The Basics)

if let airportName = airports["DUB"] {
    print("The name of the airport is \(airportName).")
} else {
    print("That airport isn't in the airports dictionary.")
}
// Prints "The name of the airport is Dublin Airport."

 

โ–ท ๋”•์…”๋„ˆ๋ฆฌ์˜ key-value ์Œ ์‚ญ์ œํ•˜๊ธฐ (nil, removeValue)
๋”•์…”๋„ˆ๋ฆฌ์—์„œ key-value ์Œ์„ ์‚ญ์ œํ•˜๊ธฐ ์œ„ํ•œ ๋ฐฉ๋ฒ•์€ ๋‘๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค.
์•„๋ž˜ ์ฝ”๋“œ๋Š” ์ฒซ๋ฒˆ์งธ๋กœ, ์ด๋ฏธ ์กด์žฌํ•˜๊ณ  ์žˆ๋Š” key๊ฐ’์˜ value๋ฅผ nil๋กœ ์ง€์ •ํ•ด์ฃผ๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค.

airports["APL"] = "Apple International"
// "Apple International" isn't the real airport for APL, so delete it
airports["APL"] = nil
// APL has now been removed from the dictionary

์ด๋Ÿฐ ๋ฐฉ๋ฒ• ๋ง๊ณ  ๋‘๋ฒˆ ์งธ๋กœ, removeValue(forKey:)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ œ๊ฑฐํ•  ์ˆ˜๋„ ์žˆ๋‹ค. removeValue(forKey:) ํ•จ์ˆ˜๋„ ์‚ญ์ œ๋œ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ด์ฃผ๋Š”๋ฐ, ๋ฌผ๋ก  ์กด์žฌํ•˜์ง€ ์•Š๋Š” key-value ์Œ์— ๋Œ€ํ•ด์„œ ์‚ญ์ œํ•˜๋ ค๊ณ  ํ•˜๋ฉด nil์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

if let removedValue = airports.removeValue(forKey: "DUB") {
    print("The removed airport's name is \(removedValue).")
} else {
    print("The airports dictionary doesn't contain a value for DUB.")
}
// Prints "The removed airport's name is Dublin Airport."

 

Iterating Over a Dictionary

๋”•์…”๋„ˆ๋ฆฌ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ, for-in ๋ฃจํ”„๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋“  ๊ฐ’์„ ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ๋‹ค. key์™€ value ๋‘๊ฐœ์˜ ๊ฐ’์— ์ ‘๊ทผํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— (key, value) ํ˜•ํƒœ์˜ ํŠœํ”Œ๋กœ ๋ฐ˜ํ™˜๋œ๋‹ค.

for (airportCode, airportName) in airports {
    print("\(airportCode): \(airportName)")
}
// LHR: London Heathrow
// YYZ: Toronto Pearson

๋ฌผ๋ก  ํŠœํ”Œ ํ˜•ํƒœ๋กœ key, value ๋‘๊ฐœ์˜ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๊ณ  ์‹ถ์ง€ ์•Š๋‹ค๋ฉด, ๋”•์…”๋„ˆ๋ฆฌ์˜ keys, values ํ”„๋กœํผํ‹ฐ๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๊ฐ๊ฐ์— ์ ‘๊ทผํ•˜๊ณ  ๋ฐ˜ํ™˜ํ•˜๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. (๋”•์…”๋„ˆ๋ฆฌ key ๋ฐ˜ํ™˜ํ•˜๊ธฐ, value๋ฐ˜ํ™˜ํ•˜๊ธฐ!)

for airportCode in airports.keys {
    print("Airport code: \(airportCode)")
}
// Airport code: LHR
// Airport code: YYZ

for airportName in airports.values {
    print("Airport name: \(airportName)")
}
// Airport name: London Heathrow
// Airport name: Toronto Pearson

keys, values ํ”„๋กœํผํ‹ฐ๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๋ฐฐ์—ด๋กœ๋„ ์ƒ์„ฑ ๊ฐ€๋Šฅํ•˜๋‹ค.

let airportCodes = [String](airports.keys)
// airportCodes is ["LHR", "YYZ"]

let airportNames = [String](airports.values)
// airportNames is ["London Heathrow", "Toronto Pearson"]

์•„ ๊ทธ๋ฆฌ๊ณ  ๋งˆ์ง€๋ง‰์œผ๋กœ, ๋”•์…”๋„ˆ๋ฆฌ ๋˜ํ•œ ์ง‘ํ•ฉ์ฒ˜๋Ÿผ ์ˆœ์„œ๊ฐ€ ์—†์ด ์ €์žฅ๋˜๋Š” ์ปฌ๋ ‰์…˜ ํƒ€์ž…์ด๋ฏ€๋กœ, sorted() ๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๋”•์…”๋„ˆ๋ฆฌ ๋‚ด ์ •๋ ฌ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

 

๋งˆ๋ฌด๋ฆฌ

์ด๋ฏธ ์†์„ฑ๊ฐ•์˜๋กœ ๋ฌธ๋ฒ•์„ ๋‹ค ๋ฐฐ์› ์ง€๋งŒ ์ด๋ ‡๊ฒŒ ํ•˜๋‚˜์”ฉ ์ง‘์–ด์„œ ๋‹ค์‹œ ๊ณต๋ถ€ํ•ด๋ณด๋‹ˆ ์ƒ‰๋‹ค๋ฅธ ๋Š๋‚Œ์ด๋‹ค. ๋” ์ •ํ™•ํ•˜๊ณ  ๋ช…ํ™•ํ•œ ์ดํ•ด๋ฅผ ์œ„ํ•ด ์ง€๋ฃจํ•˜๋”๋ผ๋„ ๊พธ์ค€ํžˆ ๊ณต์‹๋ฌธ์„œ๋ฅผ ํ•˜๋‚˜์”ฉ ์ •๋ฆฌํ•ด๋‚˜๊ฐ€๋ณด๊ฒ ๋‹ค. ๋‚ด๊ฐ€ ์›ํ•˜๋Š” ์•ฑ์„ ์ž์œ ์ž์žฌ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ์„ ๋•Œ๊นŒ์ง€ ํ™”์ดํŒ…!

๋ฐ˜์‘ํ˜•