๐Ÿฆ… Swift

Collection Types

Kiwi๐Ÿ’ป 2022. 5. 22. 20:45

Swift์—๋Š” 3๊ฐœ(Array,Set,Dictionary)์˜ ์ฝœ๋ ‰์…˜ ํƒ€์ž…์ด ์žˆ๋‹ค.

Swift์—์„œ ๋ฐฐ์—ด, ์ง‘ํ•ฉ, ๋”•์…”๋„ˆ๋ฆฌ๋Š” ์–ธ์ œ๋‚˜ ์ €์žฅํ•˜๊ณ ์ž ํ•˜๋Š” ํ‚ค๊ฐ’์ด๋‚˜, ํƒ€์ž…์— ๋Œ€ํ•ด ๋ช…ํ™•ํ•˜๋‹ค.

NOTEโ—๏ธ
Swift์˜ ๋ฐฐ์—ด,์ง‘ํ•ฉ,๋”•์…”๋„ˆ๋ฆฌ์˜ ํƒ€์ž…์€ ์ œ๋„ค๋ฆญ์œผ๋กœ ๊ตฌํ˜„๋œ๋‹ค.

์ฝœ๋ ‰์…˜์˜ ๊ฐ€๋ณ€์„ฑ (Mutablility of Collections)

์ฝœ๋ ‰์…˜ ์ƒ์„ฑํ•˜๊ณ  ๋ณ€์ˆ˜์— ํ• ๋‹น ํ–ˆ์„ ๊ฒฝ์šฐ ๋ณ€๊ฒฝ(์ถ”๊ฐ€,์‚ญ์ œ,๋ณ€๊ฒฝ) ๊ฐ€๋Šฅํ•˜๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ƒ์ˆ˜์— ํ• ๋‹นํ•˜๋ฉด ๋ณ€๊ฒฝ์ด ๋ถˆ๊ฐ€๋Šฅ ํ•˜๋‹ค.

NOTEโ—๏ธ
์ฝœ๋ ‰์…˜์„ ๋ณ€๊ฒฝํ•  ํ•„์š”๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ์—๋Š” ๋ณ€๊ฒฝ ์ƒ์ˆ˜๋กœ ์ฝœ๋ ‰์…˜์„ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ์ฝ”๋“œ์— ๋Œ€ํ•ด ๋” ์‰ฝ๊ฒŒ ์ถ”๋ก ํ•  ์ˆ˜ ์žˆ๊ณ  Swift ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ์ƒ์„ฑํ•œ ์ฝœ๋ ‰์…˜์˜ ์„ฑ๋Šฅ์„ ์ตœ์ ํ™” ํ•  ์ˆ˜ ์žˆ๋‹ค.

๋ฐฐ์—ด (Arrays)

๋ฐฐ์—ด (array)์€ ๊ฐ๊ฐ์˜ ๊ฐ’์— ์ˆœ์„œ์— ๋”ฐ๋ฅธ Index๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ ๊ฐ™์€ ๊ฐ’์ด ํ•˜๋‚˜์— ๋ฐฐ์—ด์•ˆ์— ๋‹ค๋ฅธ ์ˆœ์„œ๋กœ ์ €์žฅ ๋  ์ˆ˜ ์žˆ๋‹ค.

๋ฐฐ์—ด ํƒ€์ž… ๊ตฌ๋ฌธ (Array Type Shorthand Syntax)

๋ฐฐ์—ด ์„ ์–ธ Array<ํƒ€์ž…> , [Element]

๋นˆ๋ฐฐ์—ด ์„ ์–ธ

var someInts: [Int] = []
let emptyArray2 = Array<Int>()
let emptyArray3 = [Int]()
print("someInts is of type [Int] with \(someInts.count) items.")
// Prints "someInts is of type [Int] with 0 items."

๊ธฐ๋ณธ๊ฐ’ ๋ฐฐ์—ด ์ƒ์„ฑ (Creating an Array with a Default Value)

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

๋ฐฐ์—ด ์ ‘๊ทผ๊ณผ ์ˆ˜์ • (Accessing and Modifying an Array

numsArray.count
numsArray.isEmpty

numsArray.contains(1)  //ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๊ฐ’์„ ์ „๋‹ฌ
numsArray.contains(8)

numsArray.randomElement()

numsArray.swapAt(0, 1)

numsArray = [1, 2, 3, 4, 5]   // ๋ฐฐ์—ด ๋ฆฌํ„ฐ๋Ÿด
stringArray = ["Apple", "Swift", "iOS", "Hello"]


// [] ์„œ๋ธŒ์Šคํฌ๋ฆฝํŠธ ๋ฌธ๋ฒ•(๋Œ€๊ด„ํ˜ธ๋ฅผ ์ด์šฉํ•œ ํŠน๋ณ„ํ•œ ํ•จ์ˆ˜)
stringArray[0]
stringArray[1]
stringArray[2]
stringArray[3]

stringArray[1] = "Steve"
print(stringArray)

stringArray.first   // ๋ฆฌํ„ด๊ฐ’ String?  ====> ๋นˆ ๋ฐฐ์—ด์ด๋ผ๋ฉด nil ๋ฆฌํ„ด
stringArray.last

// ๋ฐฐ์—ด์˜ ์‹œ์ž‘ ์ธ๋ฑ์Šค
stringArray.startIndex

stringArray.endIndex
//stringArray.endIndex.advanced(by: -1)

stringArray[stringArray.startIndex]
stringArray[stringArray.endIndex - 1]
//stringArray[stringArray.index(before: stringArray.endIndex)]
//stringArray.index(<#T##i: Int##Int#>, offsetBy: <#T##Int#>)

stringArray.firstIndex(of: "iOS")     // ์•ž์—์„œ ๋ถ€ํ„ฐ ์ฐพ์•˜์„๋•Œ "iOS"๋Š” ๋ฐฐ์—ด์˜ (์•ž์—์„œ๋ถ€ํ„ฐ) ๋ช‡๋ฒˆ์งธ
stringArray.lastIndex(of: "iOS")     // ๋’ค์—์„œ ๋ถ€ํ„ฐ ์ฐพ์•˜์„๋•Œ "iOS"๋Š” ๋ฐฐ์—ด์˜ (์•ž์—์„œ๋ถ€ํ„ฐ) ๋ช‡๋ฒˆ์งธ

if let index = stringArray.firstIndex(of: "iOS") {
    print(index)
}

var alphabet = ["A", "B", "C", "D", "E", "F", "G"]


// ์•ž ๋˜๋Š” ์ค‘๊ฐ„์— ์‚ฝ์ž…ํ•˜๊ธฐ
alphabet.insert("c", at: 4)

//alphabet.insert("c", at: alphabet.endIndex)

alphabet.insert(contentsOf: ["a","b","c"], at: 0)

//alphabet.insert(contentsOf: ["a","b","c"], at: alphabet.endIndex)

  • ๊ต์ฒดํ•˜๊ธฐ (replace)

alphabet = ["A", "B", "C", "D", "E", "F", "G"]

// ์š”์†Œ ๊ต์ฒดํ•˜๊ธฐ
alphabet[0] = "a"

// ๋ฒ”์œ„๋ฅผ ๊ต์ฒดํ•˜๊ธฐ
alphabet[0...2] = ["x", "y", "z"]

// ์›ํ•˜๋Š” ๋ฒ”์œ„ ์‚ญ์ œ
alphabet[0...1] = []
alphabet

// ๊ต์ฒดํ•˜๊ธฐ ํ•จ์ˆ˜ ๋ฌธ๋ฒ•
alphabet.replaceSubrange(0...2, with: ["a", "b", "c"])

  • ์ถ”๊ฐ€ํ•˜๊ธฐ (append)

alphabet = ["A", "B", "C", "D", "E", "F", "G"]
alphabet += ["H"]

// ์ถ”๊ฐ€ํ•˜๊ธฐ ํ•จ์ˆ˜ ๋ฌธ๋ฒ•
alphabet.append("H")   // ๋งจ ๋งˆ์ง€๋ง‰์— ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ
alphabet.append(contentsOf: ["H", "I"])

//alphabet.append(7)   // ์—๋Ÿฌ ===> ๋™์ผํ•œ ์ž๋ฃŒํ˜•๋งŒ ์ €์žฅ๊ฐ€๋Šฅํ•จ

  • ์‚ญ์ œ(์ œ๊ฑฐ)ํ•˜๊ธฐ (remove)

alphabet = ["A", "B", "C", "D", "E", "F", "G"]

// ์„œ๋ธŒ์Šคํฌ๋ฆฝํŠธ ๋ฌธ๋ฒ•์œผ๋กœ ์‚ญ์ œ
alphabet[0...2] = []   //๋นˆ๋ฐฐ์—ด ์ „๋‹ฌํ•˜๋ฉด ํ•ด๋‹น ๋ฒ”์œ„๊ฐ€ ์‚ญ์ œ
// ์š”์†Œ ํ•œ๊ฐœ ์‚ญ์ œ
alphabet.remove(at: 2)  // ์‚ญ์ œํ•˜๊ณ , ์‚ญ์ œ๋œ ์š”์†Œ ๋ฆฌํ„ด
//alphabet.remove(at: 8)    // ์ž˜๋ชป๋œ ์ธ๋ฑ์Šค ์ „๋‹ฌ ====> ์—๋Ÿฌ๋ฐœ์ƒ
// ์š”์†Œ ๋ฒ”์œ„ ์‚ญ์ œ
alphabet.removeSubrange(0...2)

alphabet = ["A", "B", "C", "D", "E", "F", "G"]

alphabet.removeFirst()   // ๋งจ ์•ž์— ์š”์†Œ ์‚ญ์ œํ•˜๊ณ  ์‚ญ์ œ๋œ ์š”์†Œ ๋ฆฌํ„ด (๋ฆฌํ„ดํ˜• String)
alphabet.removeFirst(2)   // ์•ž์˜ ๋‘๊ฐœ์˜ ์š”์†Œ ์‚ญ์ œ ===> ๋ฆฌํ„ด์€ ์•ˆํ•จ

alphabet.removeLast()   // ๋งจ ๋’ค์— ์š”์†Œ ์‚ญ์ œํ•˜๊ณ  ์‚ญ์ œ๋œ ์š”์†Œ ๋ฆฌํ„ด (๋ฆฌํ„ดํ˜• String)
alphabet.removeLast(2)

// ๋ฐฐ์—ด์ด ๋น„์–ด์žˆ๋Š”์ง€๋„ ์ž˜ ํ™•์ธํ•ด๋ณด๊ณ  ์‚ญ์ œ(์ œ๊ฑฐ)ํ•ด์•ผํ•จ =======> ์—๋Ÿฌ
// alphabet.removeFirst()    // ๋ฆฌํ„ดํ˜• String
// alphabet.removeLast()     // ๋ฆฌํ„ดํ˜• String
// ๋ฐฐ์—ด์˜ ์š”์†Œ ๋ชจ๋‘ ์‚ญ์ œ(์ œ๊ฑฐ)
alphabet.removeAll()
alphabet.removeAll(keepingCapacity: true)  // ์ €์žฅ๊ณต๊ฐ„์„ ์ผ๋‹จ์€ ๋ณด๊ด€ํ•ด ๋‘ . (์•ˆ์˜ ๋ฐ์ดํ„ฐ๋งŒ ์ผ๋‹จ ๋‚ ๋ฆผ)

  • ๋ฐฐ์—ด์˜ ๊ธฐํƒ€ ๊ธฐ๋Šฅ


var nums = [1, 2, 3, 1, 4, 5, 2, 6, 7, 5, 0]
// ๋ฐฐ์—ด์„ ์ •๋ฆฌํ•ด๋ณด๊ธฐ
// 1) ๋ฐฐ์—ด์„ ์ง์ ‘์ •๋ ฌํ•˜๋Š” ๋ฉ”์„œ๋“œ  sort  (๋™์‚ฌ)
// 2) ์ •๋ ฌ๋œ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋ฆฌํ„ด  sorted (๋™์‚ฌing/๋™์‚ฌed)

nums.sort()   // ๋ฐฐ์—ด์„ ์ง์ ‘ ์ •๋ ฌ (์˜ค๋ฆ„์ฐจ์ˆœ) (๋ฐฐ์—ด๋ฆฌํ„ด ํ•˜์ง€ ์•Š์Œ)
nums.sorted()    //์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ

nums.reverse()   // ์š”์†Œ๋ฅผ ์—ญ์ˆœ์œผ๋กœ ์ •๋ ฌ  (๋ฐฐ์—ด๋ฆฌํ„ด ํ•˜์ง€ ์•Š์Œ)
nums.reversed()

nums.sorted().reversed()
// ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์€ ์ƒ์„ฑํ•˜์ง€ ์•Š๊ณ , ๋ฐฐ์—ด์˜ ๋ฉ”๋ชจ๋ฆฌ๋Š” ๊ณต์œ ํ•˜๋ฉด์„œ ์—ญ์ˆœ์œผ๋กœ ์—ด๊ฑฐํ•  ์ˆ˜ ์žˆ๋Š” ํ˜•์‹์„ ๋ฆฌํ„ด
// ๋ฐฐ์—ด์„ ๋žœ๋ค์œผ๋กœ ์„ž๊ธฐ
//nums.shuffle()    // ์š”์†Œ์˜ ์ˆœ์„œ ๋žœ๋ค์œผ๋กœ ์ง์ ‘ ๋ฐ”๊พธ๊ธฐ
//nums.shuffled()

  • ๋ฐฐ์—ด์˜ ๋น„๊ต

let a = ["A", "B", "C"]
let b = ["a", "b", "c"]

// ๋‘ ๋ฐฐ์—ด์„ ๋น„๊ตํ•ด๋ณด๊ธฐ

a == b   // false
a != b   // true

// ๊ฐœ๋ณ„์š”์†Œ ๋น„๊ต, ์ €์žฅ์ˆœ์„œ๋„ ๋น„๊ต ํ•˜๊ธฐ ๋•Œ๋ฌธ์—

  • ํ™œ์šฉ

// ํŠน์ • ์š”์†Œ ํ•œ๊ฐœ ์‚ญ์ œํ•˜๊ธฐ
// ===> ์ง์ ‘ ์‚ญ์ œ๋Š” ๋ถˆ๊ฐ€ (1) ์ธ๋ฑ์Šค ์ฐพ์•„์„œ, (2) ์ธ๋ฑ์Šค์— ํ•ด๋‹นํ•˜๋Š” ์š”์†Œ ์ง€์šฐ๊ธฐ
var puppy1 = ["p", "u", "p", "p", "y"]

if let lastIndexOfP = puppy1.lastIndex(of: "p") {
    puppy1.remove(at: lastIndexOfP)
}

if !nums.isEmpty {            // "!"(true ====> false)  (false ====> true)
    print("\(nums.count) element(s)")
} else {
    print("empty array")
}

// ๋ฐฐ์—ด์˜ ๋ฐฐ์—ด ์ ‘๊ทผ

var data = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

data[0][2]

  • ๋ฐ˜๋ณต๋ฌธ๊ณผ์˜ ๊ฒฐํ•ฉ

// ๊ฐ ์š”์†Œ์— ์ฐจ๋ก€๋Œ€๋กœ ์ ‘๊ทผ, ๋ฐฐ์—ด๊ณผ ๋ฐ˜๋ณต๋ฌธ์€ ํ•„์—ฐ์ ์œผ๋กœ ๋งŽ์ด ์“ฐ์ž„

for i in nums {
    print(i)
}

//:> enumerate: ์˜์–ด๋กœ ์—ด๊ฑฐํ•˜๋‹ค๋Š” ๋œป

// enumerated() ===> ์—ด๊ฑฐ๋œ ๊ฒƒ๋“ค์„ Named ํŠœํ”Œ ํ˜•ํƒœ๋กœ ํ•œ๊ฐœ์”ฉ ์ „๋‹ฌ

// ๋ฐ์ดํ„ฐ ๋ฐ”๊ตฌ๋‹ˆ์ด๊ธฐ ๋•Œ๋ฌธ์—,
// ์‹ค์ œ ๋ฒˆํ˜ธํ‘œ(index)๊นŒ์ง€ ๋ถ™์—ฌ์„œ, ์ฐจ๋ก€๋Œ€๋กœ ํ•˜๋‚˜์”ฉ ๊บผ๋‚ด์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์„ ์ˆ˜ ์žˆ์–ด์„œ ์•„๋ž˜์ฒ˜๋Ÿผ ํ™œ์šฉ ๊ฐ€๋Šฅ

nums = [10, 11, 12, 13, 14]

// (offset: 0, element: 10)

for tuple in nums.enumerated() {
    //print(tuple)
    print("\(tuple.0) - \(tuple.1)")
}

for (index, word) in nums.enumerated() {      // ๋ฐ”๋กœ ๋ฝ‘์•„๋‚ด๊ธฐ
    print("\(index) - \(word)")
}

for (index, value) in nums.enumerated().reversed() {      // ๊ฑฐ๊พธ๋กœ ๋’ค์—์„œ ๋ถ€ํ„ฐ
    print("\(index) - \(value)")
}

์ง‘ํ•ฉ (Sets)

์ง‘ํ•ฉ์€ ๋ฐฐ์—ด๊ณผ ๋‹ฌ๋ฆฌ ์ˆœ์„œ์™€ ์ƒ๊ด€์—†์ด ๊ฐ’์„ ์ €์žฅํ•œ๋‹ค. ์ˆœ์„œ๊ฐ€ ์ค‘์š”ํ•˜์ง€ ์•Š๊ฑฐ๋‚˜ ๊ฐ’์ด ์ค‘๋ณต๋˜๋ฉด ์•ˆ๋  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.

์ง‘ํ•ฉ ํƒ€์ž…์„ ์œ„ํ•œ ํ•ด์‰ฌ ๊ฐ’ (Hash Values for Set Types

์ง‘ํ•ฉ์˜ ๊ฐ’์€ Hashableํ•ด์•ผ ํ•œ๋‹ค. ํ•ด์‰ฌ ๊ฐ’์€ ์ •์ˆ˜๋กœ์จ ๋ชจ๋“  ๊ฐ์ฒด์— ๋Œ€ํ•ด ๋™์ผํ•˜๋‹ค. Swift์˜ ๋ชจ๋“  ๊ธฐ๋ณธ ํƒ€์ž…์€ ๊ธฐ๋ณธ์ ์œผ๋กœ Hashableํ•˜๊ณ  ์ง‘ํ•ฉ์˜ ๊ฐ’ ๋˜๋Š” ๋”•์…”๋„ˆ๋ฆฌ์˜ ํ‚ค ๊ฐ’์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ๋˜ํ•œ ์—ฐ๊ด€๊ฐ’์ด ์—†๋Š” ์—ด๊ฑฐํ˜• ์ผ€์ด์Šค ๊ฐ’์€ ๊ธฐ๋ณธ์ ์œผ๋กœ Hashable ํ•˜๋‹ค.

๋นˆ์ง‘ํ•ฉ ์„ ์–ธ

var letters = Set<Character>()
let emptySet: Set<Int> = []
print("letters is of type Set<Character> with \(letters.count) items.")
// Prints "letters is of type Set<Character> with 0 items."

์ง‘ํ•ฉ ์ ‘๊ทผ๊ณผ ์ˆ˜์ • (Accessing and Modifying a Set)


  • Set์˜ ๊ธฐ๋ณธ ๊ธฐ๋Šฅ

set.count
set.isEmpty

set.contains(1)
set.randomElement()

  • ์—…๋ฐ์ดํŠธ (update) - ์‚ฝ์ž…ํ•˜๊ธฐ / ๊ต์ฒดํ•˜๊ธฐ / ์ถ”๊ฐ€ํ•˜๊ธฐ

// ์„œ๋ธŒ์Šคํฌ๋ฆฝํŠธ ๊ด€๋ จ ๋ฌธ๋ฒ• ์—†์Œ โญ๏ธ

// (์ •์‹ ๊ธฐ๋Šฅ) ํ•จ์ˆ˜ ๋ฌธ๋ฒ•

set.update(with: 1)     // Int?

set.update(with: 7)     // ์ƒˆ๋กœ์šด ์š”์†Œ๊ฐ€ ์ถ”๊ฐ€๋˜๋ฉด ====> ๋ฆฌํ„ด nil

Set๋Š” append ํ•จ์ˆ˜๋ฅผ ์ œ๊ณตํ•˜์ง€ ์•Š์Œ
append๋Š” ์ˆœ์„œ๊ฐ€ ์žˆ๋Š” ์ปฌ๋ ‰์…˜์˜ ๋์— ์ถ”๊ฐ€ํ•˜๋Š” ๊ฐœ๋…
Set์€ ์ˆœ์„œ๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์—, update๋ฅผ ํ†ตํ•ด์„œ ์ถ”๊ฐ€


  • ์‚ญ์ œ(์ œ๊ฑฐ)ํ•˜๊ธฐ (remove)

var stringSet: Set<String> = ["Apple", "Banana", "City", "Swift"]

// ์š”์†Œ ์‚ญ์ œํ•ด ๋ณด๊ธฐ
stringSet.remove("Swift")     // "Swift" ์‚ญ์ œํ•œ ์š”์†Œ๋ฅผ ๋ฆฌํ„ด
stringSet                     // ["Hello", "Apple"]

// ์กด์žฌํ•˜์ง€ ์•Š๋Š” ์š”์†Œ๋ฅผ ์‚ญ์ œํ•ด๋ณด๊ธฐ
stringSet.remove("Steve")       // nil    (์—๋Ÿฌ๋Š” ๋ฐœ์ƒํ•˜์ง€ ์•Š์Œ)

// ์ „์ฒด์š”์†Œ ์‚ญ์ œ
stringSet.removeAll()
stringSet.removeAll(keepingCapacity: true)

  • Set์˜ ์ •์˜

var a: Set = [1, 2, 3, 4, 5, 6, 7, 8, 9]
var b: Set = [1, 3, 5, 7, 9]
var c: Set = [2, 4, 6, 8, 10]
var d: Set = [1, 7, 5, 9, 3]

  • Set์˜ ๋น„๊ต

a == b
a != b

//Set(์ •๋ ฌ๋˜์ง€ ์•Š์€ ์ปฌ๋ ‰์…˜)์€ ์–ด์งœํ”ผ ์ˆœ์„œ๋Š” ์ค‘์š”ํ•˜์ง€ ์•Š์Œ
b == d  // ํ•ญ์ƒ true

  • ๋ถ€๋ถ„์ง‘ํ•ฉ / ์ƒ์œ„์ง‘ํ•ฉ / ์„œ๋กœ์†Œ

a = [1, 2, 3, 4, 5, 6, 7, 8, 9]
b = [1, 3, 5, 7, 9]     // ํ™€์ˆ˜ ๋ชจ์Œ
c = [2, 4, 6, 8, 10]    // ์ง์ˆ˜ ๋ชจ์Œ
d = [1, 7, 5, 9, 3]     // ํ™€์ˆ˜ ๋ชจ์Œ

// ๋ถ€๋ถ„์ง‘ํ•ฉ ์—ฌ๋ถ€๋ฅผ ํŒ๋‹จ

b.isSubset(of: a)   // true ๋ถ€๋ถ„์ง‘ํ•ฉ ์—ฌ๋ถ€
b.isStrictSubset(of: a)   // false ์ง„๋ถ€๋ถ„์ง‘ํ•ฉ ์—ฌ๋ถ€

// ์ƒ์œ„์ง‘ํ•ฉ

a.isSuperset(of: b)    // true ์ƒ์œ„์ง‘ํ•ฉ ์—ฌ๋ถ€
a.isStrictSuperset(of: b)   // false  ์ง„์ƒ์œ„์ง‘ํ•ฉ ์—ฌ๋ถ€

// ์„œ๋กœ์†Œ ์—ฌ๋ถ€

d.isDisjoint(with: c)


  • ํ•ฉ์ง‘ํ•ฉ

a = [1, 2, 3, 4, 5, 6, 7, 8, 9]
b = [1, 3, 5, 7, 9]
c = [2, 4, 6, 8, 10]
d = [1, 7, 5, 9, 3]

var unionSet =  b.union(c)

//b.formUnion(c)      // ์›๋ณธ๋ณ€๊ฒฝ

  • ๊ต์ง‘ํ•ฉ

var interSet = a.intersection(b)

//a.formIntersection(b)      // ์›๋ณธ๋ณ€๊ฒฝ

  • ์ฐจ์ง‘ํ•ฉ

var subSet = a.subtracting(b)

//a.subtract(b)       // ์›๋ณธ๋ณ€๊ฒฝ

  • ๋Œ€์นญ์ฐจ์ง‘ํ•ฉ

var symmetricSet = a.symmetricDifference(b)

//a.formSymmetricDifference(b)       // ์›๋ณธ๋ณ€๊ฒฝ

  • ๋ฐ˜๋ณต๋ฌธ๊ณผ์˜ ๊ฒฐํ•ฉ


let iteratingSet: Set = [1, 2, 3]

for num in iteratingSet {    // ์ •๋ ฌ๋˜์ง€ ์•Š์€ ์ปฌ๋ ‰์…˜์ด๊ธฐ ๋•Œ๋ฌธ์—, ์‹คํ–‰ํ• ๋•Œ๋งˆ๋‹ค ์ˆœ์„œ๊ฐ€ ๋‹ฌ๋ผ์ง
    print(num)
}

  • ๊ธฐํƒ€ ์œ ์˜์ 

var newSet: Set = [1, 2, 3, 4, 5]

var newArray: Array = newSet.sorted()

//:> Set์„ ์ •๋ ฌํ•˜๋ฉด, ๋ฐฐ์—ด๋กœ ๋ฆฌํ„ดํ•จ (์ •๋ ฌ์€ ์ˆœ์„œ๊ฐ€ ํ•„์š”ํ•˜๊ธฐ ๋•Œ๋ฌธ)

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

๋”•์…”๋„ˆ๋ฆฌ๋Š” ์ˆœ์„œ์™€ ์ƒ๊ด€์—†์ด ํƒ€์ž…์˜ ํ‚ค์™€ ๊ฐ’์œผ๋กœ ์ €์žฅํ•œ๋‹ค. ๊ฐ ๊ฐ’์€ ๋”•์…”๋„ˆ๋ฆฌ ๋‚ด๋ถ€์—์„œ ์œ ์ผํ•œ ํ‚ค๊ฐ’๊ณผ ๋Œ€์น˜๋œ๋‹ค. ํŠน์ • ๋‹จ์–ด๋ฅผ ์ฐพ๊ธฐ์œ„ํ•ด ์‚ฌ์ „์—์„œ ์ฐพ๋Š” ๋ฐฉ๋ฒ•์ฒ˜๋Ÿผ ์‹๋ณ„์ž๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๊ฐ’์„ ์ฐพ๋Š”๋‹ค.

๋นˆ ๋”•์…”๋„ˆ๋ฆฌ ์ƒ์„ฑ

 var namesOfIntegers = [Int: String]()
let emptyDic1: Dictionary<Int, String> = [:]
let emptyDic2 = Dictionary<Int, String>()
// namesOfIntegers is an empty [Int: String] dictionary

๋”•์…”๋„ˆ๋ฆฌ ์ ‘๊ทผ๊ณผ ์ˆ˜์ •


  • ๋”•์…”๋„ˆ๋ฆฌ์˜ ๊ธฐ๋ณธ ๊ธฐ๋Šฅ

dic = ["A": "Apple", "B": "Banana", "C": "City"]

dic.count
dic.isEmpty
// contains ํ•จ์ˆ˜๊ฐ€ ์กด์žฌํ•˜๊ธด ํ•˜๋‚˜, ๋’ค์—์„œ ๋ฐฐ์šธ ํด๋กœ์ €์˜ ๋‚ด์šฉ์„ ์•Œ์•„์•ผํ•จ
// dic.contains(where: <#T##((key: String, value: String)) throws -> Bool#>)

dic.randomElement()      // Named Tuple ํ˜•ํƒœ๋กœ ๋ฆฌํ„ด

  • ๋”•์…”๋„ˆ๋ฆฌ์˜ ๊ฐ ์š”์†Œ(element)์— ๋Œ€ํ•œ ์ ‘๊ทผ (ํ‚ค ๊ฐ’์œผ๋กœ ์ ‘๊ทผ)

// ๋ฐฐ์—ด
var numsArray = [1, 2, 3, 4, 5]
numsArray[0]
//:> ๋”•์…”๋„ˆ๋ฆฌ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ์„œ๋ธŒ์Šคํฌ๋ฆฝํŠธ[ ]๋ฅผ ์ด์šฉํ•œ ๋ฌธ๋ฒ•์„ ์ฃผ๋กœ ์‚ฌ์šฉ

// ๋”•์…”๋„ˆ๋ฆฌ
dic = ["A": "Apple", "B": "Banana", "C": "City"]
dic["A"]        // nil์˜ ๊ฐ€๋Šฅ์„ฑ =====> String?(์˜ต์…”๋„)
//print(dic["A"])
//print(dic["Q"])

if let a = dic["A"] {    // ์˜ต์…”๋„ ๋ฐ”์ธ๋”ฉ
    print(a)
} else {
    print("Not found")
}

// ๋”•์…”๋„ˆ๋ฆฌ๋Š” ๊ฐ’๋งŒ ๋”ฐ๋กœ ๊ฒ€์ƒ‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์ œ๊ณตํ•˜์ง€ ์•Š์Œ. โญ๏ธ
// ์„œ๋ธŒ์Šคํฌ๋ฆฝํŠธ ๋ฌธ๋ฒ•์œผ๋กœ "ํ‚ค"๋ฅผ ์ „๋‹ฌ

// ์ฐธ๊ณ  (์ž˜ ์‚ฌ์šฉํ•˜์ง€๋Š” ์•Š์Œ)
dic["S", default: "Empty"]       // nil์ด ๋ฐœ์ƒํ•  ํ™•๋ฅ ์ด ์—†์Œ
// ์ž๋ฃŒ๊ฐ€ ์—†์œผ๋ฉด ๊ธฐ๋ณธ๊ฐ’์„ ๋ฆฌํ„ดํ•˜๋Š” ๋ฌธ๋ฒ•  ===> ๋ฆฌํ„ด String

dic.keys
dic.values

dic.keys.sorted()
dic.values.sorted()

for key in dic.keys.sorted() {     // ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ  ===> "return ๋ฐฐ์—ด" ๋ฐฐ์—ด์ด ๋จ
    print(key)
}

  • ์—…๋ฐ์ดํŠธ (update) - ์‚ฝ์ž…ํ•˜๊ธฐ / ๊ต์ฒดํ•˜๊ธฐ / ์ถ”๊ฐ€ํ•˜๊ธฐ

words = [:]

words["A"] = "Apple"   // ์• ํ”Œ๋กœ ๋‹ค์‹œ ๋ฐ”๊พธ๊ธฐ
words

words["B"] = "Banana"  // ๋™์ผํ•œ ํ‚ค๊ฐ€ ์—†์œผ๋ฉด ===> ์ถ”๊ฐ€ํ•˜๊ธฐ
words

words["B"] = "Blue"    // ๋™์ผํ•œ ํ‚ค๊ฐ€ ์žˆ์œผ๋ฉด ===> ๊ธฐ์กด ๋ฐธ๋ฅ˜ ๋ฎ์–ด์“ฐ๊ธฐ
words
words.updateValue("City", forKey: "C")

// (์ •์‹ ๊ธฐ๋Šฅ) ํ•จ์ˆ˜ ๋ฌธ๋ฒ• (update + insert = upsert)

words.updateValue("City", forKey: "C")   // ์ƒˆ๋กœ์šด ์š”์†Œ๊ฐ€ ์ถ”๊ฐ€๋˜๋ฉด ==> ๋ฆฌํ„ด nil
words

//words = [:]      // ๋นˆ ๋”•์…”๋„ˆ๋ฆฌ๋กœ ๋งŒ๋“ค๊ธฐ
words = ["A": "A"]   // ์ „์ฒด ๊ต์ฒดํ•˜๊ธฐ(๋ฐ”๊พธ๊ธฐ)

//:> ๋”•์…”๋„ˆ๋ฆฌ๋Š” append ํ•จ์ˆ˜๋ฅผ ์ œ๊ณตํ•˜์ง€ ์•Š์Œ
// append๋Š” ์ˆœ์„œ๊ฐ€ ์žˆ๋Š” ์ปฌ๋ ‰์…˜์˜ ๋์— ์ถ”๊ฐ€ํ•˜๋Š” ๊ฐœ๋…
// ๋”•์…”๋„ˆ๋ฆฌ๋Š” ์ˆœ์„œ๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์—, update๋ฅผ ํ†ตํ•ด์„œ ์ถ”๊ฐ€

  • ์‚ญ์ œ(์ œ๊ฑฐ)ํ•˜๊ธฐ (remove)

dic = ["A": "Apple", "B": "Banana", "C": "City"]

// ์š”์†Œ ์‚ญ์ œํ•ด ๋ณด๊ธฐ

dic["B"] = nil    // ํ•ด๋‹น์š”์†Œ ์‚ญ์ œ
dic

dic["E"] = nil   // ์กด์žฌํ•˜์ง€ ์•Š๋Š” ํ‚ค/๊ฐ’์„ ์‚ญ์ œ ======> ์•„๋ฌด์ผ์ด ์ผ์–ด๋‚˜์ง€ ์•Š์Œ(์—๋Ÿฌ์•„๋‹˜)
dic

// ํ•จ์ˆ˜๋กœ ์‚ญ์ œํ•ด๋ณด๊ธฐ
dic.removeValue(forKey: "A")   // ์‚ญ์ œํ›„, ์‚ญ์ œ๋œ ๊ฐ’ ๋ฆฌํ„ด

dic.removeValue(forKey: "A")   // ๋‹ค์‹œ ์‚ญ์ œํ•ด๋ณด๊ธฐ ===> nil๋ฆฌํ„ด

// ์ „์ฒด ์‚ญ์ œํ•˜๊ธฐ

dic.removeAll()
dic.removeAll(keepingCapacity: true)

  • ๋”•์…”๋„ˆ๋ฆฌ์˜ ๋น„๊ต

let a = ["A": "Apple", "B": "Banana", "C": "City"]
let b = ["A": "Apple", "C": "City", "B": "Banana"]

// ๋น„๊ต ์—ฐ์‚ฐ์ž
a == b   // true
// ๋”•์…”๋„ˆ๋ฆฌ๋Š” ์›๋ž˜ ์ˆœ์„œ๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์—, ์ˆœ์„œ๋Š” ์ƒ๊ด€์—†์Œ
// (์ˆœ์„œ ์ƒ๊ด€์—†์ด ๋ฌด์กฐ๊ฑด true๋‚˜์˜ด - Hashableํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ์ˆœ์„œ ์ƒ๊ด€์—†์ด ๋น„๊ต๊ฐ€๋Šฅ)

a != b   // false

  • ๋”•์…”๋„ˆ๋ฆฌ์˜ ํ™œ์šฉ

// ๋”•์…”๋„ˆ๋ฆฌ์˜ ์ค‘์ฒฉ ์‚ฌ์šฉ
var dict1 = [String: [String]]()        // ๋”•์…”๋„ˆ๋ฆฌ ๋ฐธ๋ฅ˜์— "๋ฐฐ์—ด"์ด ๋“ค์–ด๊ฐˆ ์ˆ˜๋„ ์žˆ๋‹ค.

dict1["arr1"] = ["A", "B", "C"]
dict1["arr2"] = ["D", "E", "F"]
dict1
//dict1["arr"] = "A"

var dict2 = [String: [String: Int]]()     // ๋”•์…”๋„ˆ๋ฆฌ ์•ˆ์— ๋”•์…”๋„ˆ๋ฆฌ๊ฐ€ ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ๋‹ค.

dict2["dic1"] = ["name": 1, "age": 2]
dict2["dic2"] = ["name": 2, "age": 4]
dict2

  • ๋ฐ˜๋ณต๋ฌธ๊ณผ์˜ ๊ฒฐํ•ฉ

// ๋”•์…”๋„ˆ๋ฆฌ ์—ด๊ฑฐํ•ด์„œ ํ”„๋ฆฐํŠธ ํ•˜๊ธฐ

let dict = ["A": "Apple", "B": "Banana", "C": "City"]

// ๋”•์…”๋„ˆ๋ฆฌ๋Š” ์—ด๊ฑฐํ•˜์ง€ ์•Š์•„๋„, Named ํŠœํ”Œ ํ˜•ํƒœ๋กœ ํ•˜๋‚˜์‹ ๊บผ๋‚ด์„œ ์ „๋‹ฌ
// ์ˆœ์„œ๊ฐ€ ์—†์œผ๋ฏ€๋กœ, ์‹คํ–‰๋งˆ๋‹ค ์ˆœ์„œ๊ฐ€ ๋‹ฌ๋ผ์ง โญ๏ธ

// ๋ฐ์ดํ„ฐ ๋ฐ”๊ตฌ๋‹ˆ์ด๊ธฐ ๋•Œ๋ฌธ์—,
// ์ฐจ๋ก€๋Œ€๋กœ ํ•˜๋‚˜์”ฉ ๊บผ๋‚ด์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์„ ์ˆ˜ ์žˆ์–ด์„œ ์•„๋ž˜์ฒ˜๋Ÿผ ํ™œ์šฉ ๊ฐ€๋Šฅ

for (key, value) in dict {
    print("\(key): \(value)")
}

for (key, _) in dict {
    print("Key :", key)
}

for (_, value) in dict {
    print("Value :", value)
}

์ฐธ๊ณ : ์•จ๋Ÿฐ Swift๋ฌธ๋ฒ• ๋งˆ์Šคํ„ฐ ์Šค์ฟจ ์ฝ”๋“œ์ž๋ฃŒ

'๐Ÿฆ… Swift' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

Functions  (0) 2022.06.01
Control Flow  (0) 2022.06.01
Strings and Characters  (0) 2022.05.22
Basicย Operators  (0) 2022.05.15
The Basics  (0) 2022.05.15