์ƒˆ์†Œ์‹

๐Ÿ“ฑ iOS/-- Swift 5

(Swift) Collection - 2. Set(์ง‘ํ•ฉ), 3. Dictionary(์‚ฌ์ „)

  • -

Collection - 2. Set (์ง‘ํ•ฉ)

Set

  • set ํ˜•ํƒœ๋กœ ์ง€์ •๋˜๋ ค๋ฉด ๋ฐ˜๋“œ์‹œ ํƒ€์ž…์ด hashable์ด์–ด์•ผ๋งŒ ํ•œ๋‹ค.
  • swift์˜ hashableํƒ€์ž…์€์€ Int Double Bool String ๋“ฑ์ด ์žˆ๋‹ค.
  • set์—๋Š” ์ค‘๋ณต๋œ ์›์†Œ๊ฐ€ ์žˆ์„ ์ˆ˜ ์—†๋‹ค. ๋งŒ์•ฝ ๋„ฃ๋”๋ผ๋„ ์ค‘๋ณต๊ฐ’์€ ์‚ฌ๋ผ์ง„๋‹ค.
  • set๋„ array์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ค‘๊ด„ํ˜ธ [ ]๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
  • set์€ array์™€ ๋‹ค๋ฅด๊ฒŒ index๋กœ ์ ‘๊ทผ ํ•  ์ˆ˜ ์—†๋‹ค. (์ˆœ์„œ๊ฐ€ ์—†๋‹ค)
  • ์ง‘ํ•ฉ๊ณผ ๊ฐ™์€ ๊ฐœ๋…

 

Set ์ƒ์„ฑ

var letters = Set<Character>()

 

์›์†Œ ์ถ”๊ฐ€

letters.insert("A")

 

๋ฐฐ์—ด ๋ฆฌํ„ฐ๋Ÿด์„ ์ด์šฉํ•œ set ์ƒ์„ฑ

var musicGenres : Set<String> = ["Hip hop", "Classic", "Rock"]

 

Set์˜ ์ถ”๊ฐ€ / ์‚ญ์ œ / ์ ‘๊ทผ

var language : set<String> = ["python", "c", "java"]
language.insert("swift")
if language.isEmpty {
  print("language is Empty!")
} else{
  if language.contains("swift"){
    print("language contains swift!")
  } else{
    print("language not contains swift")
  }
}

>>> language contains swift!
  • isEmptyํ•จ์ˆ˜๋กœ ๋น„์–ด์žˆ๋Š”์ง€ ํ™•์ธ ๊ฐ€๋Šฅํ•˜๋‹ค.
  • insertํ•จ์ˆ˜๋กœ ๊ฐ’์„ ์ถ”๊ฐ€ ํ•œ๋‹ค.
  • containsํ•จ์ˆ˜๋กœ ํ•ด๋‹น ๊ฐ’์ด ์žˆ๋Š”์ง€ ํ™•์ธ ๊ฐ€๋Šฅํ•˜๋‹ค.

 

์ง‘ํ•ฉ์—ฐ์‚ฐ (๊ต์ง‘ํ•ฉ, ์ฐจ์ง‘ํ•ฉ, ๋Œ€์นญ์ฐจ, ํ•ฉ์ง‘ํ•ฉ)

Swift - set ์—ฐ์‚ฐ

  • intersection : ๊ต์ง‘ํ•ฉ
  • symmetricDifference : ๋Œ€์นญ์ฐจ์ง‘ํ•ฉ (ํ•ฉ์ง‘ํ•ฉ์—์„œ ๊ต์ง‘ํ•ฉ์„ ๋บ€๊ฒƒ)
  • union : ํ•ฉ์ง‘ํ•ฉ
  • subtracting : ์ฐจ์ง‘ํ•ฉ
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]

์ด ์™ธ์—๋„ ๋ถ€๋ถ„์ง‘ํ•ฉ์ธ์ง€ ํ™•์ธํ•˜๋Š” isSuperset(of:), isStrictSubset(of:) ๋“ฑ์˜ ๋ฉ”์†Œ๋“œ๊ฐ€ ์žˆ๋‹ค.

Swift ์˜ Dictionaryํƒ€์ž…์€ Foundationํด๋ž˜์Šค์˜ NSDictionary๋ฅผ ๋ธŒ๋ฆฟ์ง€ํ•œ ํƒ€์ž…์ด๋‹ค.

 

 

Collection - 3. Dictionary(์‚ฌ์ „)

 

  • Swift ์˜ Dictionaryํƒ€์ž…์€ Foundationํด๋ž˜์Šค์˜ NSDictionary๋ฅผ ๋ธŒ๋ฆฟ์ง€ํ•œ ํƒ€์ž…์ด๋‹ค.
  • [key : Value] ํ˜•ํƒœ๋กœ ์„ ์–ธํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ์„ ์–ธํ• ๋•Œ๋Š” key์˜ ํƒ€์ž…๊ณผ Value์˜ ํƒ€์ž…์„ ๊ฐ๊ฐ ๋ช…์‹œํ•ด์•ผํ•œ๋‹ค.
  • array์ฒ˜๋Ÿผ ์ธ๋ฑ์Šค๋กœ ์ ‘๊ทผํ•˜๋Š”๊ฒŒ ์•„๋‹ˆ๋ผ, key๋ฅผ ํ†ตํ•ด ์ ‘๊ทผํ•œ๋‹ค.
  •  

Dictionary ์ƒ์„ฑ

var nameOfIntegers = [Int : String]()
nameOfIntegers[16] = "sixteen"
print(nameOfIntegers)
>>> [16: "sixteen"]

 

๋ฆฌํ„ฐ๋Ÿด์„ ์ด์šฉํ•œ Dictionary ์ƒ์„ฑ

var priceOfItem : [String: Int] = ["banana":1000, "ice":500, "soju":1800]
print(priceOfItem)
>>> ["banana": 1000, "ice": 500, "soju": 1800]
  • isEmpty๋ฅผ ์ด์šฉํ•˜์—ฌ ๋นˆ ๋”•์…”๋„ˆ๋ฆฌ์ธ์ง€ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.
  • Dictionary[key] = Value ์˜ ํ˜•ํƒœ๋กœ ์ƒˆ๋กœ์šด key, Value๋ฅผ ์ถ”๊ฐ€ ํ•  ์ˆ˜ ์žˆ๋‹ค.
Contents

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.