๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
  • Dev Blog
iOS/Swift

[Swift] - ์ฝœ๋ ‰์…˜ ํƒ€์ž… (Collection Types)

by ์œ ์ง„์˜ 2023. 12. 3.

์ฐธ๊ณ  ์ž๋ฃŒ

 

์ฝœ๋ ‰์…˜ ํƒ€์ž… (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] ARC  (1) 2024.05.24
[Swift] Extension  (1) 2024.05.20
[Swift] - ๋ฌธ์ž์—ด๊ณผ ๋ฌธ์ž (Strings and Characters)  (0) 2023.12.03
[Swift] - ๊ธฐ๋ณธ ์—ฐ์‚ฐ์ž (Basic Operators)  (0) 2023.12.03
[Swift] ๊ธฐ๋ณธ (The Basics)  (0) 2023.12.03

๋Œ“๊ธ€