์ปฌ๋ ์ ํ์ ( Collection Types )
์๋ ํ์ธ์!! ์ค๋์ swift์์์ ์ปฌ๋ ์ ํ์ ์ ๊ดํด์ ์ ๋ฆฌ๋ฅผ ํด๋ณด๋ ค๊ณ ํฉ๋๋ค.
Collection Types — The Swift Programming Language (Swift 5.7)
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
์ปฌ๋ ์ ํ์
์ปฌ๋ ์ ํ์ ์ด๋ ์ฌ๋ฌ ๊ฐ์ ๋ฐ์ดํฐ๋ฅผ ๋ด์ ์ ์๋ ๋ฐ์ดํฐ ๊ตฌ์กฐ์ ๋๋ค.
Swift์์๋ ์ปฌ๋ ์ ํ์ ์ผ๋ก
- Array : ์์๊ฐ ์๋ ๋ฆฌ์คํธ ์ปฌ๋ ์
- Set : ์์๊ฐ ์๊ณ , ๋ฉค๋ฒ๊ฐ ์ ์ผํ ์ปฌ๋ ์
- Dictionary : ํค ์ ๊ฐ์ ์์ผ๋ก ์ด๋ฃจ์ด์ง ์ปฌ๋ ์
โป Array, Set, Dictionary์ ๋ณ์์ธ var๋ก ํ ๋นํ๋ฉด ์ปฌ๋ ์ ์ ๋ณ๊ฒฝ์ด ๊ฐ๋ฅํ๊ณ ์์์ธ let๋ก ํ ๋นํ๋ฉด ๋ณ๊ฒฝ์ด ๋ถ๊ฐํฉ๋๋ค.
์ปฌ๋ ์ ์ ๋ณ๊ฒฝํ ํ์๊ฐ ์๋ ๋ชจ๋ ๊ฒฝ์ฐ์ ๋ณ๊ฒฝํ ์ ์๋ ์ปฌ๋ ์ ์ ๋ง๋๋ ๊ฒ์ด ์ข์ต๋๋ค.
์ฝ๋์ ๋ํด ์ฝ๊ฒ ์ถ๋ก ์ด ๊ฐ๋ฅํ๋ฉฐ Swift ์ปดํ์ผ๋ฌ๊ฐ ์์ฑํ ์ปฌ๋ ์ ์ ์ฑ๋ฅ์ ์ต์ ํํ ์ ์์ต๋๋ค.
๋ฐฐ์ด ( Array )
Swift์์์ ๋ฐฐ์ด์ ๋์ผํ ๊ฐ์ ์์๊ฐ ์ง์ ๋ ๊ฐ์ ๋ชฉ๋ก์ ์ ์ฅํ๋ ๋ฐ ์ฌ์ฉํ ์ ์์ต๋๋ค.
Swift๋ ์ค์๋ก๋ ์๋ชป๋ ํ์ ์ ๊ฐ์ ๋ฐฐ์ด์ ์ ๋ ฅํ ์ ์๋๋ก ์๊ฒฉํ๊ฒ ๊ฒ์ฌํฉ๋๋ค.
Array๋ ๋ฉค๋ฒ๊ฐ ์์(์ธ๋ฑ์ค)๋ฅผ ๊ฐ์ง ๋ฆฌ์คํธ ํํ์ ์ปฌ๋ ์ ํ์ ์ ๋๋ค.
๋น ๋ฐฐ์ด์ ์์ฑ
์ด๊ธฐํ ๊ตฌ๋ฌธ์ ์ฌ์ฉํ์ฌ Intํ ๋น ๋ฐฐ์ด์ ๋ง๋ค ์ ์์ต๋๋ค.
var someInts = [Int]()
print("someInts is of type [Int] with \(someInts.count) items.")
// someInts is of type [Int] with 0 items.
// Int ๋ฐฐ์ด
let numberArray = [2, 4, 6, 8, 10, 12, 14]
// String ๋ฐฐ์ด
let stringArray = ["Swift", "Hello", "World"]
// Double ๋ฐฐ์ด
var doubleArray: [Double] = [0.1, 0.3, 0.5]
// Float ๋ฐฐ์ด
var floatArray = [Float]() // ๋น์ด์์
someInts.append(3)
// ๋ฐฐ์ด์ 3์ ์ถ๊ฐ ํ์ต๋๋ค.
someInts = []
// ๋ฐฐ์ด์ ๋น์ ์ต๋๋ค. ๋ฐฐ์ด์ ์์ดํ
ํ์
์ ๊ทธ๋๋ก Int๋ก ์ ์ง๋ฉ๋๋ค.
append(_: ) : ์ธ์คํด์ค ๋ฉ์๋๋ก append๋ฅผ ์ฌ์ฉํ๋ฉด ๋ณ๊ฒฝ ๊ฐ๋ฅํ ๋ฐฐ์ด์ ๋์ ๋จ์ผ ์์๋ฅผ ์ถ๊ฐํฉ๋๋ค.
๊ธฐ๋ณธ ๊ฐ์ผ๋ก ๋น ๋ฐฐ์ด ์์ฑ
repeating ๋ฉ์๋์ count ๋ฉ์๋๋ฅผ ์ด์ฉํด ๊ธฐ๋ณธ ๊ฐ์ผ๋ก ๋น ๋ฐฐ์ด์ ์์ฑํ ์ ์์ต๋๋ค.
var threeDoubles = Array(repeating: 0.0, count: 3)
// threeDoubles : Double ํ์
์ [0.0, 0.0, 0.0]
init(repeating : count: ) : ๋จ์ผ ๋ฐ๋ณต ๊ฐ์ ์ง์ ๋ ์๋ฅผ ํฌํจํ๋ ์ ์ปฌ๋ ์ ์ ๋ง๋ญ๋๋ค. repeatedValue๋ ๋ฐ๋ณตํ ์์์ด๋ฉฐ , count๋ ๋งค๊ฐ๋ณ์์ ์ ๋ฌ๋ ๊ฐ์ ๋ฐ๋ณตํ ํ์์ด๋ฉฐ 0 ์ด์์ด์ด์ผ ํฉ๋๋ค.
๋ค๋ฅธ ๋ฐฐ์ด์ ์ถ๊ฐํ ๋ฐฐ์ด์ ์์ฑ
+ ์ฐ์ฐ์๋ฅผ ์ด์ฉํด ๋ฐฐ์ด์ ํ ์น ์ ์์ต๋๋ค.
var anotherThreeDoubles = Array(repeating: 2.5, count: 3)
// anotherThreeDoubles : [2.5, 2.5, 2.5]
var sixDoubles = threeDoubles + anotherThreeDoubles
// sixDoubles : [0.0, 0.0, 0.0, 2.5, 2.5, 2.5]
๋ฆฌํฐ๋ด์ ์ด์ฉํ ๋ฐฐ์ด์ ์์ฑ
[value 1, value 2, value 3] ํํ๋ฅผ ์ด์ฉํ์ฌ ๋ฐฐ์ด์ ์์ฑํ ์ ์์ต๋๋ค.
var shoppingList: [String] = ["Eggs", "Milk"]
// ๋์ฑ ๊ฐ๋จํ๊ฒ ์ ์ธํ๊ธฐ
var shoppingList = ["Eggs", "Milk"]
๋ฐฐ์ด์ ์ ๊ทผ ๋ฐ ๋ณํ
๋ฐฐ์ด์ ์์ ๊ฐ์๋ฅผ ํ์ธํ ์ ์์ต๋๋ค.
print(shoppingList.count)
// 2
๋ฐฐ์ด์ด ๋น์๋์ง ํ์ธ
if shoppingList.isEmpty {
print("is empty.")
} else {
print("is not empty.")
}
// is not empty.
isEmpty : ๋ฌธ์์ด์ ๋ฌธ์๊ฐ ์๋์ง ์ฌ๋ถ๋ฅผ ๋ํ๋ด๋ ๋ถ์ธ ๊ฐ์ ๋๋ค.
๋ฐฐ์ด์ ์์๋ฅผ ์ถ๊ฐ
shoppingList.append("Four")
// shoppingList.count = 3
shoppingList += ["Baking Powder"]
// shoppingList.count = 4
shoppingList += ["Chocolate Spread", "Cheese", "Butter"]
// shoppingList.count = 7
print(shoppingList)
//"["Eggs", "Milk", "Four", "Baking Powder", "Chocolate Spread", "Cheese", "Butter"]
๋ฐฐ์ด์ ํน์ ์์น ์์ ์ ๊ทผ
//0๋ฒ์จฐ ์ธ๋ฑ์ค ๋ฐฐ์ด์ ๊ฐ์ ์ถ๋ ฅ
var firstItem = shoppingList[0]
// firstItem : "Eggs"
//2๋ฒ์จฐ ์ธ๋ฑ์ค ๋ฐฐ์ด์ ๊ฐ์ ์ถ๋ ฅ
var threeItem = shoppingList[2]
// firstItem : "Four"
shoppingList[4...6] = ["Bananas", "Apples"]
// 4, 5, 6๋ฒ์งธ ์ธ๋ฑ์ค ์์ดํ
์ Banana, Apples๋ก ๋ณํ
//"Chocolate Spread", "Cheese", "Butter" ๊ฐ๋ค์ด = > "Banana", "Apples"์ผ๋ก ๋ณํ
ํน์ ์์น์ ์์ ์ถ๊ฐ / ์ญ์ / ์ ๊ทผ
//shoppingList ์ฒซ๋ฒ์งธ ์์น์ "Maple Syrup"์ ๋ฃ์ต๋๋ค.
shoppingList.insert("Maple Syrup", at:0)
// shoppingList ์ฒซ๋ฒ์งธ ์์์์ ํ๋๋ฅผ ์ ๊ฑฐ ํฉ๋๋ค.
let mapleSyrup = shoppingList.remove(at: 0)
firstItem = shoppingList[0]
// firstItem : "Eggs"
let apples = shoppingList.removeLast()
removeLast () : ์ปฌ๋ ์ ์ ๋ง์ง๋ง ์์๋ฅผ ๋ฐํํฉ๋๋ค.
๋ฐฐ์ด์ ์ํ
for-in loop์ ์ด์ฉํด ๋ฐฐ์ด์ ์ํํ ์ ์์ต๋๋ค.
for item in shoppingList {
print(item)
}
// Eggs
// Milk
// Four
// Baking Powder
// Bananas
๋ฐฐ์ด์ ๊ฐ๊ณผ ์ธ๋ฑ์ค๊ฐ ํ์ํ ๋๋ enumerated( ) ๋ฉ์๋๋ฅผ ์ฌ์ฉํฉ๋๋ค.
for (index, value) in shoppingList.enumerated() {
print("Item \(index + 1): \(value)")
}
// Item 1: Eggs
// Item 2: Milk
// Item 3: Four
// Item 4: Baking Powder
// Item 5: Bananas
enumerated() : ์์ ์ํ์ค ( n ,x )๋ฅผ ๋ฐํํฉ๋๋ค. ์ฌ๊ธฐ์ n์ 0์์ ์์ํ๋ ์ฐ์์ ์ธ ์ ์๋ฅผ ๋ํ๋ด๊ณ x๋ ์ํ์ค์ ์์๋ฅผ ๋ํ๋ ๋๋ค.
์ ( Set )
Set์ ๋์ผํ ํ์ ์ ๊ณ ์ ํ ๊ฐ์ ์ ์ฅํ๋ ๋ฐ ์ฌ์ฉ๋์ง๋ง ๋ฐฐ์ด๊ณผ ๋ค๋ฅด๊ฒ ๋ช ํํ ์์๊ฐ ์์ต๋๋ค. ํญ๋ชฉ์ ์์๊ฐ ์์ต๋๋ค. ํญ๋ชฉ์ ์์๊ฐ ์ค์ํ์ง ์๊ฑฐ๋ ์ค๋ณต ๊ฐ์ด ์๋์ง ํ์ธํ๋ ค๋ ๊ฒฝ์ฐ์๋ ๋ฐฐ์ด ๋์ Set์ ์ด์ฉํ ์ ์์ต๋๋ค. Set๋ ์์๊ฐ ์๊ณ ๋ฉค๋ฒ๊ฐ ์ ์ผํ ๊ฒ์ ๋ณด์ฅํ๋ ์ปฌ๋ ์ ํ์ ๋๋ค. Set ํํ๋ก ์ ์ฅ๋๊ธฐ ์ํด์๋ ๋ฐ๋์ ํ์ ์ด hashable์ด์ด์ผ๋ง ํฉ๋๋ค. Swift์์ String, Int, Double, Bool ๊ฐ์ ๊ธฐ๋ณธ ํ์ ์ ๊ธฐ๋ณธ์ ์ผ๋ก hashable์ ๋๋ค. Swift์์ Setํ์ ์ Set์ผ๋ก ์ ์ธํฉ๋๋ค.
๋น Set ์์ฑ
Set<Element>๋ก ์์ฑํ๋ฉฐ ์ด๊ธฐํ ๋ฌธ๋ฒ์ ์ฌ์ฉํด์ ํน์ ํ์ ์ ๋น์ด์๋ Set๋ฅผ ๋ง๋ค ์ ์์ต๋๋ค.
var letters = Set<Character>()
print(letters.count)
// 0
letters.insert("a")
letters = []
๋ฐฐ์ด ๋ฆฌํฐ๋ด์ ์ด์ฉํ Set ์์ฑ
var favoriteGenres: Set<String> = ["Rock", "Classical", "Hip hop"]
↓ Swift์ ํ์ ์ถ๋ก ์ผ๋ก ์๋์ ๊ฐ์ด ์ ์ธ๋ ๊ฐ๋ฅํฉ๋๋ค.
var favoriteGenres: Set = ["Rock", "Classical", "Hip hop"]
Set์ ์ ๊ทผ๊ณผ ๋ณ๊ฒฝ
isEmpty๋ฅผ ์ด์ฉํ์ฌ favoriteGenres์์ ์์๋ค์ด ๋น์๋์ง ํ์ธ
if favoriteGenres.isEmpty {
print("is Empty")
} else {
print("is not Empty")
}
// is not Empty
insert๋ฅผ ์ด์ฉํด์ favoriteGenres "Jazz" ์์๋ฅผ ๋ฃ์ด์ค๋๋ค.
favoriteGenres.insert("Jazz")
remove์ ์ด์ฉํ์ฌ favoriteGenres ์์ ์๋ "Rock" ์ด๋ผ๋ ์์๋ฅผ ์ง์์ค๋๋ค.
if let removedGenre = favoriteGenres.remove("Rock") {
print("\(removedGenre)? remove")
} else {
print("I never much cared for that.")
}
// Rock? remove
"Funk" ๋ผ๋ ๊ฐ์ด ์๋์ง ํ์ธํฉ๋๋ค.
if favoriteGenres.contains("Funk") {
print("I get up on the good foot.")
} else {
print("It's too funky in here.")
}
// It's too funky in here.
contains (_: ) : ์ํ์ค์ ์ง์ ๋ ์์๊ฐ ํฌํจ๋์ด ์๋์ง ์ฌ๋ถ๋ฅผ ๋ํ๋ด๋ boolean๊ฐ์ ๋ฐํํฉ๋๋ค.
Set์ ์ํ
for-in loop์ ์ด์ฉํด Set์ ์ํํ ์ ์์ต๋๋ค.
for genre in favoriteGenres {
print("\(genre)")
}
// Classical
// Hip hop
// Jazz
Set ๋ช ๋ น
let oddDigits: Set = [1, 3, 5, 7, 9]
let evenDigits: Set = [0, 2, 4, 6, 8]
let singleDigitPrimeNumbers: Set = [2, 3, 5, 7]
//2๊ฐ์ ํจ์์ ์๋ ๋ชจ๋ ๊ฐ์ผ๋ก ์๋ก์ด ํจ์๋ฅผ ๋ง๋ญ๋๋ค.
oddDigits.union(evenDigits).sorted()
// [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
//2๊ฐ์ ํจ์์์ ๊ณตํต ๊ฐ๋ง์ผ๋ก ์๋ก์ด ํจ์๋ฅผ ๋ง๋ญ๋๋ค.
oddDigits.intersection(evenDigits).sorted()
// []
//ํน์ ํจ์์ ์๋ ๊ฐ์ผ๋ก ์๋ก์ด ํจ์๋ฅผ ๋ง๋ญ๋๋ค.
oddDigits.subtracting(singleDigitPrimeNumbers).sorted()
// [1, 9]
// 2๊ฐ์ ํจ์์์ ๊ณตํต ๊ฐ์ ์ ์ธํ ๊ฐ๊ฐ์ ์ธํธ์๋ง ์๋ ๊ฐ์ผ๋ก ์๋ก์ด ํจ์ ๋ง๋ญ๋๋ค.
oddDigits.symmetricDifference(singleDigitPrimeNumbers).sorted()
// [1, 2, 9]
๐ union / intersection / subtracting / symmetricDifference
Set์ ๋ฉค๋ฒ์ญ๊ณผ ๋๋ฑ ๋น๊ต
Set์ ๋๋ฑ ๋น๊ต์ ๋ฉค๋ฒ ์ฌ๋ถ๋ฅผ ํ์ธํ๊ธฐ ์ํด ๋ฉ์๋๋ฅผ ์ฌ์ฉํฉ๋๋ค.
- ==
- isSuperset(of: ) : ์ฃผ์ด์ง ์งํฉ์ ์์ ์งํฉ์ธ์ง ์ฌ๋ถ๋ฅผ ๋ํ๋ด๋ boolean๊ฐ์ ๋ฐํํฉ๋๋ค.
- isStrictSubset(of: ) : ์งํฉ์ด ์ฃผ์ด์ง ์ํ์ค์ ์๊ฒฉํ ํ์ ์งํฉ์ธ์ง ์ฌ๋ถ๋ฅผ ๋ํ๋ด๋ boolean๊ฐ์ ๋ฐํํฉ๋๋ค.
- isStrictSuperset(of :) : ์งํฉ์ด ์ฃผ์ด์ง ์ํ์ค์ ์๊ฒฉํ ์์ ์งํฉ์ธ์ง ์ฌ๋ถ๋ฅผ ๋ํ๋ด๋ boolean๊ฐ์ ๋ฐํํฉ๋๋ค.
- isDisjoint(with : ) : ์งํฉ์ ์ฃผ์ด์ง ์งํฉ๊ณผ ๊ณตํต๋๋ ๊ตฌ์ฑ์์ด ์๋์ง ์ฌ๋ถ๋ฅผ ๋ํ๋ด๋ boolean๊ฐ์ ๋ฐํํฉ๋๋ค.
let houseAnimals: Set = ["๐ถ", "๐ฑ"]
let farmAnimals: Set = ["๐ฎ", "๐", "๐", "๐ถ", "๐ฑ"]
let cityAnimals: Set = ["๐ฆ", "๐ญ"]
houseAnimals.isSubset(of: farmAnimals)
// true
farmAnimals.isSuperset(of: houseAnimals)
// true
farmAnimals.isDisjoint(with: cityAnimals)
// true
์ฌ์ ( Dictionaries )
๋์ ๋๋ฆฌ๋ ์ ๋ ฌ๋์ง ์์ ๋์ผํ ํ์ ์ ๊ฐ์ ๋ชฉ๋ก์ ์ ์ฅํ๋ ๋ฐ์ ์ฌ์ฉํ ์ ์์ต๋๋ค. ๋์ ๋๋ฆฌ๋ key๋ผ๋ ๊ณ ์ ์๋ณ์๋ฅผ ์ฌ์ฉํฉ๋๋ค.
๋์ผํ key๋ฅผ ํตํด ๊ฐ์ ์ฐธ์กฐํ๊ณ ์กฐํํ ์ ์์ต๋๋ค. key๋ ์ ์๋ ๋ฌธ์์ด์ด ๋ ์ ์์ผ๋ฉฐ ๋์ ๋๋ฆฌ ๋ด์์ ๊ณ ์ ํ ๊ฐ์ด์ด์ผ ํฉ๋๋ค.
๋์ ๋๋ฆฌ๋ฅผ ๋ณ์์ ํ ๋นํ๋ฉด ํญ์ ๋ณ๊ฒฝ ๊ฐ๋ฅํ๋ฉฐ ์์์ ํ ๋นํ๋ฉด ๋ณ๊ฒฝํ ์ ์์ต๋๋ค.
[ key : Value ] ํํ๋ก Dictionary๋ฅผ ์ ์ธํด ์ฌ์ฉํ ์ ์์ต๋๋ค.
โป Swift์ Dictionaryํ์ ์ Foundation ํด๋์ค์ NSDictionary๋ฅผ bridgeํ ํ์ ์ ๋๋ค.
๋น Dictionary์ ์์ฑ
var namesOfIntegers = [Int: String]()
Key๊ฐ์ Intํ์ด๊ณ Value๋ Stringํ์ผ๋ก ์ ์ธํ์ต๋๋ค.
namesOfIntegers[16] = "sixteen"
namesOfIntegers = [:]
// ๋น ์ฌ์
๋ฆฌํฐ๋ด์ ์ด์ฉํ Dictionary์ ์์ฑ
[ key 1: value 1, key 2: value 2, key 3: value 3, key 4: value 4 ]ํํ๋ก ๋์ ๋๋ฆฌ๋ฅผ ์ ์ธํ ์ ์์ต๋๋ค.
var airports: [String: String] = ["YYZ": "Toronto Pearson", "DUB": "Dublin"]
Dictionary์ ์ ๊ทผ๊ณผ ๋ณ๊ฒฝ
print(airports.count)
// 2
count๋ฅผ ํตํด airports์์ ๋ช ๊ฐ์ ๊ฐ์ด ์๋์ง ํ์ธํ์ต๋๋ค.
if airports.isEmpty {
print("is empty.")
} else {
print(" is not empty.")
}
// is not empty.
isEmpty๋ฅผ ์ด์ฉํ์ฌ ๋์ ๋๋ฆฌ๊ฐ ๋น์๋์ง ํ์ธํฉ๋๋ค.
airports["LHR"] = "London"
// the airports dictionary now contains 3 items
๋์ ๋๋ฆฌ ๊ฐ์ ํ ๋นํด์ค๋๋ค.
์ปฌ๋ ์ ํ์ ์ ๊ดํ์ฌ ์ ๋ฆฌ๋ฅผ ํ์ต๋๋ค. ์ ๋ฆฌ๋ฅผ ํ๋ ์ค๊ฐ์ ์๋ ์ ์ฅ์ด ์ ๋์ด ์ ๋ฐ์ด ๋ ์๊ฐ ๐ก ํ...
๋ค์๋ถํด ๊พธ์คํ ์์์ ์ฅ์ ๋๋ฌ๊ฐ๋ฉด์ ๊ธ์ ์ ๋ฆฌํด์ผ ๋ ๊ฑฐ ๊ฐ์ต๋๋ค!!!