๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป iOSun
article thumbnail

์ดˆ๋ฐ˜์€  ๊ธฐ๋ณธ์ ์œผ๋กœ ์•„๋Š” ์ง€์‹ ์€ ๊ฐ„๋‹จํ•˜๊ฒŒ ์ •๋ฆฌํ•˜๊ฑฐ๋‚˜ ํŒจ์Šคํ•˜๊ณ   ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ ๋œ ์ง€์‹  ์œ„์ฃผ๋กœ ์ •๋ฆฌํ•ด ๋ณด๋ ค ํ•ฉ๋‹ˆ๋‹ค.

์ฝœ๋ ‰์…˜ ํƒ€์ž…(Collection Types)

  • ๋ฐฐ์—ด, ์„ธํŠธ(์ง‘ํ•ฉ), ๋”•์…”๋„ˆ๋ฆฌ 3๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค.
  • ์ €์žฅํ•˜๋Š” ๊ฐ’์˜ ํƒ€์ž…์ด ๋ช…ํ™•ํ•˜๋‹ค.
    • ex) Intํƒ€์ž…์˜ ๋ฐฐ์—ด์ด๋ฉด Int๊ฐ’๋งŒ ๋„ฃ์„ ์ˆ˜ ์žˆ๋‹ค.

๋ฐฐ์—ด(Array)

var arr1: [Int] = [] // ์งง์€ ํ‘œํ˜„ ์„ ํ˜ธ
var arr2: Array<Int> = []

var map = Array(repeating: Array(repeating: 0, count: 3), count: 2)
// [[0, 0, 0], [0, 0, 0]]
// 0 0 0
// 0 0 0
  • ๋ฐฐ์—ด์˜ ์„ ์–ธ ๋ฐฉ๋ฒ•: [Element], Array<Element>
  • ๊ธฐ๋ณธ ๊ฐ’์œผ๋กœ ์„ค์ •ํ•˜๊ณ  ํฌ๊ธฐ๋ฅผ ๊ณ ์ •ํ•˜์—ฌ ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•˜๋Š” ์ดˆ๊ธฐํ™”๋„ ๊ฐ€๋Šฅ
  • ex) ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€ ๋•Œ map ๋งŒ๋“ค ๋•Œ ์ž์ฃผ ์“ฐ๊ฒŒ ๋œ๋‹ค.

 

var shoppingList = ["Eggs", "Milk", "Baking Powder", "Cheese"]
shoppingList[1...3] = ["Bananas", "Apples", "Candy"]
print(shoppingList) // ["Eggs", "Bananas", "Apples", "Candy"]
  • ์„œ๋ธŒ ์Šคํฌ๋ฆฝํŠธ ๊ตฌ๋ฌธ์œผ๋กœ ๋ฒ”์œ„ ์•ˆ์— ๊ฐ’์„ ํ•œ๋ฒˆ์— ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚˜๋ฉด ๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ๊ฐ€ ๋œฌ๋‹ค.

 

์ง‘ํ•ฉ(Set)

  • ์ˆœ์„œ๊ฐ€ ์ค‘์š”ํ•˜์ง€ ์•Š๊ฑฐ๋‚˜ ์ค‘๋ณต๋œ ๊ฐ’์„ ์ œ๊ฑฐํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.
  • Set์˜ ํƒ€์ž…์€ ๋ฐ˜๋“œ์‹œ Hashable ํ•ด์•ผํ•œ๋‹ค.
    • Hashable์ด๋ž€?
      • Swift์˜ ๋ชจ๋“  ๊ธฐ๋ณธ ํƒ€์ž… (String, Int, Double, Bool)์€ ๊ธฐ๋ณธ์ ์œผ๋กœ hashable์ด๋‹ค.
      • ๊ธฐ๋ณธ ํƒ€์ž…์ด ์•„๋‹ˆ๋ฉด Hashable ํ”„๋กœํ† ์ฝœ์„ ์ฑ„ํƒํ•˜์ž (์ฐธ๊ณ )

 

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

// ํ•˜๋‚˜์˜ ํƒ€์ž…์˜ ๊ฐ’์ด ํฌํ•จ๋˜์–ด ์žˆ๋Š” ๊ฒฝ์šฐ ์ง‘ํ•ฉ ์š”์†Œ์˜ ํƒ€์ž…์„ ๋ช…์‹œํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค.
var favoriteGenres: Set = ["Rock", "Classical", "Hip hop"] 

  • Set ํƒ€์ž…์€ ๋ช…์‹œ์ ์œผ๋กœ ์„ ์–ธํ•ด์•ผ ํ•œ๋‹ค. but ํ•˜๋‚˜์˜ ํƒ€์ž…์˜ ๊ฐ’์ด ํฌํ•จ๋˜์–ด ์žˆ๋Š” ๊ฒฝ์šฐ → ์ง‘ํ•ฉ ์š”์†Œ์˜ ํƒ€์ž…์„ ๋ช…์‹œํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค.

 

์ง‘ํ•ฉ ์—ฐ์‚ฐ ์ˆ˜ํ–‰

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]

intersection(_:) ๋ฉ”์„œ๋“œ

  • ๋‘ ์ง‘ํ•ฉ์˜ ๊ณตํ†ต ๊ฐ’์„ ๊ฐ€์ง€๋Š” ์ƒˆ๋กœ์šด ์ง‘ํ•ฉ์„ ์ƒ์„ฑํ•œ๋‹ค.

symmetricDifference(_:) ๋ฉ”์„œ๋“œ

  • ๋‘ ์ง‘ํ•ฉ์˜ ๊ณตํ†ต ๊ฐ’์„ ์ œ์™ธํ•œ ์ƒˆ๋กœ์šด ์ง‘ํ•ฉ์„ ์ƒ์„ฑํ•œ๋‹ค.

union(_:) ๋ฉ”์„œ๋“œ

  • ๋‘ ์ง‘ํ•ฉ์˜ ๋ชจ๋“  ๊ฐ’์œผ๋กœ ์ƒˆ๋กœ์šด ์ง‘ํ•ฉ์„ ์ƒ์„ฑํ•œ๋‹ค.

subtracting(_:) ๋ฉ”์„œ๋“œ

  • ํŠน์ • ์ง‘ํ•ฉ์˜ ๊ณตํ†ต ๊ฐ’์„ ์ œ์™ธํ•œ ์ƒˆ๋กœ์šด ์ง‘ํ•ฉ์„ ์ƒ์„ฑํ•œ๋‹ค.

 

let houseAnimals: Set = ["๐Ÿถ", "๐Ÿฑ"]
let farmAnimals: Set = ["๐Ÿฎ", "๐Ÿ”", "๐Ÿ‘", "๐Ÿถ", "๐Ÿฑ"]
let cityAnimals: Set = ["๐Ÿฆ", "๐Ÿญ"]

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

isSubset(of:) ๋ฉ”์„œ๋“œ

  • ํŠน์ • ์ง‘ํ•ฉ์— ๋ชจ๋“  ๊ฐ’์ด ํฌํ•จ๋˜์–ด ์žˆ๋Š”์ง€ ํŒ๋‹จ

isSuperset(of:) ๋ฉ”์„œ๋“œ

  • ์ง‘ํ•ฉ์— ํŠน์ • ์ง‘ํ•ฉ์— ๋ชจ๋“  ๊ฐ’์„ ํฌํ•จ ํ•˜๋Š”์ง€ ํŒ๋‹จ

isStrictSubset(of:) ๋˜๋Š” isStrictSuperset(of:) ๋ฉ”์„œ๋“œ

  • ๋ถ€๋ถ„์ง‘ํ•ฉ์ธ์ง€ ์ดˆ์ง‘ํ•ฉ์ธ์ง€ ํŒ๋‹จ

isDisjoint(with:) ๋ฉ”์„œ๋“œ

  • ๋‘ ์ง‘ํ•ฉ์ด ๊ณตํ†ต ๊ฐ’์ด ์—†๋Š”์ง€ ํŒ๋‹จ

 

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

  • ์ˆœ์„œ์™€ ์ƒ๊ด€์—†์ด ์ฝœ๋ ‰์…˜์— ๊ฐ™์€ ํƒ€์ž…์˜ ํ‚ค (key)์™€ ๊ฐ™์€ ํƒ€์ž…์˜ ๊ฐ’ (value)๋ฅผ ์ €์žฅ
  • ๋”•์…”๋„ˆ๋ฆฌ Key ํƒ€์ž…์€ ๋ฐ˜๋“œ์‹œ Hashable ํ”„๋กœํ† ์ฝœ์„ ์ค€์ˆ˜ํ•ด์•ผ ํ•œ๋‹ค.

 

updateValue ๋ฉ”์†Œ๋“œ

var colors = ["red": "apple", "blue": "sea"]
colors.updateValue("phone", forKey: "black")
print(colors) // ["black": "phone", "red": "apple", "blue": "sea"]
  • ํ•ด๋‹น ํ‚ค๊ฐ€ ์žˆ์œผ๋ฉด value๊ฐ’์„ updateํ•œ๋‹ค, oldValue๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค.
  • ํ•ด๋‹น ํ‚ค๊ฐ€ ์—†์œผ๋ฉด ์ƒˆ๋กœ ์—…๋ฐ์ดํŠธ ํ•ด์ค€๋‹ค, nil์„ ๋ฆฌํ„ดํ•œ๋‹ค.

 

let colorsKeys = [String](colors.keys) // ["blue", "red"]
let colorsValues = [String](colors.values) // ["sea", "apple"]
  • ํ‚ค ๋˜๋Š” ๊ฐ’๋งŒ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด๋กœ ์ดˆ๊ธฐํ™” ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • but ๋”•์…”๋„ˆ๋ฆฌ์—์„œ ๊ฐ€์ ธ์˜จ ๊ฐ’์ด๊ธฐ ๋•Œ๋ฌธ์— ์ˆœ์„œ๋Š” ์—†๋‹ค.

๋งŒ์•ฝ์— [String] ์„ ์ƒ๋žตํ•œ๋‹ค๋ฉด

  • Dictionary.Keys๋ผ๋Š” ๊ตฌ์กฐ์ฒด ํƒ€์ž…์ด๋‹ค.
  • ๋‹น์—ฐํžˆ ์„œ๋ธŒ์Šคํฌ๋ฆฝํŠธ๋กœ ์ ‘๊ทผ์ด ๋˜์ง€ ์•Š๋Š”๋‹ค.
profile

๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป iOSun

@iosun

ํฌ์ŠคํŒ…์ด ์ข‹์•˜๋‹ค๋ฉด "์ข‹์•„์š”โค๏ธ" ๋˜๋Š” "๊ตฌ๋…๐Ÿ‘๐Ÿป" ํ•ด์ฃผ์„ธ์š”!

profile on loading

Loading...