์ฐธ๊ณ ์๋ฃ
์ฝ๋ ์ ํ์ (Collection Types) - Swift
let oddDigits: Set = [1, 3, 5, 7, 9] let evenDigits: Set = [0, 2, 4, 6, 8] let singleDigitPrimeNumbers: Set = [2, 3, 5, 7] // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
bbiguduk.gitbook.io
Swift ๊ณต์ ๋ฌธ์๋ฅผ ํ๊ตญ์ด๋ก ๋ฒ์ญํ ์ฌ์ดํธ์ ๋๋ค.
๐ป ์ฐธ๊ณ ์๋ฃ์ ๋ชจ๋ ๋ด์ฉ์ ๋ค๋ฃจ๋ ๊ฒ์ด ์๋๋ผ ์๋กญ๊ฒ ์๊ฒ ๋ ์ ํน์ ๋ค๋ฅธ ํ๋ก๊ทธ๋๋ฐ ์ธ์ด๋ค๊ณผ ๋น๊ตํ์ ๋ ๋ค๋ฅธ ์ ๋ค์ ์์ฃผ๋ก ์ ๋ฆฌํ์ต๋๋ค. ๋ํ, ์ฐธ๊ณ ์๋ฃ ์ด์ธ์๋ ์ถ๊ฐ์ ์ผ๋ก ๊ณต๋ถํ ๋ถ๋ถ๋ ์ ๋ฆฌํ์ต๋๋ค.
Array
Array๋ ๋ฉค๋ฒ๊ฐ ์์(์ธ๋ฑ์ค)๋ฅผ ๊ฐ์ง ๋ฆฌ์คํธ ํํ์ ์ปฌ๋ ์ ํ์ ์ด๋ค.
๊ธฐ๋ณธ ๊ฐ์ผ๋ก ๋น ๋ฐฐ์ด ์์ฑ ๋ฐ ๋ฐฐ์ด ํฉ์น๊ธฐ
repeating, count ์ด๋์ ๋ผ์ด์ ๋ก ๊ธฐ๋ณธ ๊ฐ์ผ๋ก ๋น ๋ฐฐ์ด์ ์์ฑํ ์ ์๊ณ + ์ฐ์ฐ์๋ฅผ ์ด์ฉํด ๋ฐฐ์ด์ ํฉ์น ์ ์๋ค.
var threeDoubles = Array(repeating: 0.0, count: 3)
// threeDoubles is of type [Double], and equals [0.0, 0.0, 0.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]
๋ฐฐ์ด์ ์ ๊ทผ ๋ฐ ๋ณํ
๋ฒ์ ์ฐ์ฐ์๋ฅผ ์ฌ์ฉํด์ ์ ๊ทผ ๋ฐ ๋ณํ์ ํ ์ ์๋ค.
var shoppingList: [String] = ["Eggs", "Milk", "Flour", "Baking Powder",
"Chocolate Spread", "Cheese", "Butter", "Bread"]
shoppingList[4...6] = ["Bananas", "Apples"]
// 4, 5, 6๋ฒ์งธ ์ธ๋ฑ์ค ์์ดํ
์ Banana, Apples๋ก ๋ณํ
// ["Eggs", "Milk", "Flour", "Baking Powder", "Bananas", "Apples", "Bread"]
ํน์ ์์น์ ์์ ์ถ๊ฐ/์ญ์ /์ ๊ทผ
shoppingList.insert("Maple Syrup", at:0)
let mapleSyrup = shoppingList.remove(at: 0)
// Maple Syrup์ด ๋ฐํ
firstItem = shoppingList[0]
// firstItem : "Six eggs"
let apples = shoppingList.removeLast()
// ๋ง์ง๋ง ์ธ๋ฑ์ค์ ๊ฐ์ด ๋ฐํ
๋ฐฐ์ด์ ์ํ
๋ฐฐ์ด์ ๊ฐ๊ณผ ์ธ๋ฑ์ค๊ฐ ํ์ํ ๋๋ enumerated() ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ค.
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
Set
์์๊ฐ ์๊ณ , ๋ฉค๋ฒ๊ฐ ์ ์ผํ ๊ฒ์ ๋ณด์ฅํ๋ ์ปฌ๋ ์ ํ์ ์ด๋ค.
Set์ ์ ๊ทผ๊ณผ ๋ณ๊ฒฝ
var favoriteGenres: Set<String> = ["Rock", "Classical", "Hip hop"]
print("I have \(favoriteGenres.count) favorite music genres.")
// I have 3 favorite music genres.
if favoriteGenres.isEmpty {
print("As far as music goes, I'm not picky.")
} else {
print("I have particular music preferences.")
}
// I have particular preferences.
favoriteGenres.insert("Jazz")
if let removedGenre = favoriteGenres.remove("Rock") {
// remove๋ ํน์ ๋ฉค๋ฒ๊ฐ ์์ผ๋ฉด nil์ ๋ฐํํ๊ธฐ ๋๋ฌธ์ ์ต์
๋ ๋ฐ์ธ๋ฉ์ ์ด์ฉํจ
print("\(removedGenre)? I'm over it.")
} else {
print("I never much cared for that.")
}
// Rock? I'm over it.
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.
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]
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]
// oddDigits๊ณผ singleDigitPrimeNumbers์ ํฉ์งํฉ - ๊ต์งํฉ
Set์ ํฌํจ ๊ด๊ณ
let houseAnimals: Set = ["๐ถ", "๐ฑ"]
let farmAnimals: Set = ["๐ฎ", "๐", "๐", "๐ถ", "๐ฑ"]
let cityAnimals: Set = ["๐ฆ", "๐ญ"]
houseAnimals.isSubset(of: farmAnimals)
// ์ฐธ
farmAnimals.isSuperset(of: houseAnimals)
// ์ฐธ
farmAnimals.isDisjoint(with: cityAnimals)
// farmAnimals์ cityAnimals๊ฐ์ ๊ณตํต๊ฐ์ด ํ๋๋ ์์ ๊ฒฝ์ฐ ์ฐธ
// ์ฐธ
Dictionary
Dictionary๋ ํค์ ๊ฐ์ ์์ผ๋ก ์ด๋ฃจ์ด์ง ์ปฌ๋ ์ ํ์ ์ด๋ค.
Dictionary์ ์ ๊ทผ๊ณผ ๋ณ๊ฒฝ
var airports: [String: String] = ["YYZ": "Toronto Pearson", "DUB": "Dublin"]
airports["LHR"] = "London"
// ํค์ ํด๋นํ๋ ๊ฐ ํ ๋น
airports["LHR"] = "London Heathrow"
// ํค์ ํด๋นํ๋ ๊ฐ ๋ณ๊ฒฝ
if let airportName = airports["DUB"] {
print("The name of the airport is \(airportName).")
} else {
print("That airport isn't in the airports dictionary.")
}
// ํค์ ํด๋นํ๋ ๊ฐ์ ๋ค๋ฅธ ๋ณ์๋ ์์์ ํ ๋นํ๊ณ ์ ํ ๋๋ ์ต์
๋ ๋ฐ์ธ๋ฉ์ ์ฌ์ฉ
// ํค์ ํด๋นํ๋ ๊ฐ์ ์ญ์ ํ๋ ๋ ๊ฐ์ง ๋ฐฉ๋ฒ
airports["DUB"] = nil
// or
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.")
}
// removeValue๋ ํค์ ํด๋นํ๋ ๊ฐ์ด ์์ ๊ฒฝ์ฐ nil์ ๋ฐํํ๊ธฐ ๋๋ฌธ์ ์ต์
๋ ๋ฐ์ธ๋ฉ ์ฌ์ฉ
Dictionary์ ์ํ
for (airportCode, airportName) in airports {
print("\(airportCode): \(airportName)")
}
// LHR: London Heathrow
// YYZ: Toronto Pearson
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
Dictionary์ key, value์ ํ์ฉ
let airportCodes: [String] = Array(airports.keys)
// airportCodes is ["LHR", "YYZ"]
let airportNames: [String] = Array(airports.values)
// airportNames is ["London Heathrow", "Toronto Pearson"]
'iOS > Swift' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Swift] Extension (1) | 2024.10.29 |
---|---|
[Swift] Objective-C (1) | 2024.10.28 |
[Swift] - ๊ธฐ๋ณธ ์ฐ์ฐ์ (Basic Operators) (0) | 2024.10.26 |
[Swift] - ๋ฌธ์์ด๊ณผ ๋ฌธ์ (Strings and Characters) (0) | 2024.10.25 |
[Swift] ๊ธฐ๋ณธ (The Basics) (0) | 2024.10.24 |
๋๊ธ