๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป/Swift

5. ์ปฌ๋ ‰์…˜ ํƒ€์ž…(Array, Dictionary, Set) - iOS ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ์œ„ํ•œ ์Šค์œ„ํ”„ํŠธ ๊ธฐ์ดˆ

reujusong 2020. 6. 16. 15:52
  • Array - ์ˆœ์„œ๊ฐ€ ์žˆ๋Š” ๋ฆฌ์ŠคํŠธ ์ปฌ๋ ‰์…˜
  • Dictionary - 'ํ‚ค'์™€ '๊ฐ’'์˜ ์Œ์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ์ปฌ๋ ‰์…˜
  • Set - ์ˆœ์„œ๊ฐ€ ์—†๊ณ , ๋ฉค๋ฒ„๊ฐ€ ์œ ์ผํ•œ ์ปฌ๋ ‰์…˜

 

- Array

  • ๋ฉค๋ฒ„๊ฐ€ ์ˆœ์„œ(์ธ๋ฑ์Šค)๋ฅผ ๊ฐ€์ง„ ๋ฆฌ์ŠคํŠธ ํ˜•ํƒœ์˜ ์ปฌ๋ ‰์…˜ ํƒ€์ž…
  • ์—ฌ๋Ÿฌ๊ฐ€์ง€ ๋ฆฌํ„ฐ๋Ÿด ๋ฌธ๋ฒ•์„ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ์–ด ํ‘œํ˜„ ๋ฐฉ๋ฒ•์ด ๋‹ค์–‘ํ•ฉ๋‹ˆ๋‹ค.

 

- Dictionary

  • 'ํ‚ค'์™€ '๊ฐ’'์˜ ์Œ์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ์ปฌ๋ ‰์…˜ ํƒ€์ž…
  • Array์™€ ๋น„์Šทํ•˜๊ฒŒ ์—ฌ๋Ÿฌ๊ฐ€์ง€ ๋ฆฌํ„ฐ๋Ÿด ๋ฌธ๋ฒ•์„ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ์–ด ํ‘œํ˜„ ๋ฐฉ๋ฒ•์ด ๋‹ค์–‘ํ•ฉ๋‹ˆ๋‹ค.

 

- Set

  • ์ค‘๋ณต๋˜์ง€ ์•Š๋Š” ๋ฉค๋ฒ„๊ฐ€ ์ˆœ์„œ์—†์ด ์กด์žฌํ•˜๋Š” ์ปฌ๋ ‰์…˜
  • Array, Dictionary์™€ ๋‹ค๋ฅด๊ฒŒ ์ถ•์•ฝํ˜•์ด ์กด์žฌํ•˜์ง€ ์•Š์Œ

 

import Swift

//MARK: - Array
// ๋นˆ Int Array ์ƒ์„ฑ
var integers: Array<Int> = Array<Int>()
integers.append(1)
integers.append(100)
//integers.append(101.1)

print(integers) // [1, 100]
print(integers.contains(100)) // true
print(integers.contains(99)) // false
integers.remove(at: 0)
integers.removeLast()
integers.removeAll()

print(integers.count)   // 0
//integers[0] // ๋ฒ”์œ„ ์ดˆ๊ณผ - ๋Ÿฐํƒ€์ž„ ์˜ค๋ฅ˜ ๋ฐœ์ƒ
// Array<Double>์™€ [Double]๋Š” ๋™์ผํ•œ ํ‘œํ˜„
// ๋นˆ Double Array ์ƒ์„ฑ
var doubles: Array<Double> = [Double]()

// ๋นˆ String Array ์ƒ์„ฑ
var strings: [String] = [String]()

// ๋นˆ Character Array ์ƒ์„ฑ
// []๋Š” ์ƒˆ๋กœ์šด ๋นˆ Array
var characters: [Character] = []

// let์„ ์‚ฌ์šฉํ•˜์—ฌ Array๋ฅผ ์„ ์–ธํ•˜๋ฉด ๋ถˆ๋ณ€ Array
let immutableArray = [1, 2, 3]

// ๋ถˆ๋ฉด Array์˜ ์š”์†Œ๋Š” ์ถ”๊ฐ€/์‚ญ์ œ ๋ถˆ๊ฐ€ - ์ปดํŒŒ์ผ ์˜ค๋ฅ˜ ๋ฐœ์ƒ
//immutableArray.append(4)
//immutableArray.removeAll()



//MARK: - Dictionary
// Key๊ฐ€ String ํƒ€์ž…์ด๊ณ  Value๊ฐ€ Any์ธ ๋นˆ Dictionary ์ƒ์„ฑ
var anyDictionary: Dictionary<String, Any> = [String: Any]()
anyDictionary["someKey"] = "value"
anyDictionary["anotherKey"] = 100
print(anyDictionary) // ["someKey": "value", "anotherKey": 100]
// Key์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ’ ๋ณ€๊ฒฝ
anyDictionary["someKey"] = "dictionary"
print(anyDictionary) // ["someKey": "dictionary", "anotherKey": 100]
// Key์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ’ ์ œ๊ฑฐ
anyDictionary.removeValue(forKey: "anotherKey")
anyDictionary["someKey"] = nil
print(anyDictionary) // [:]
// ๋นˆ Dictionary ์ƒ์„ฑ
let emptyDictionary: [String: String] = [:]

// ์ดˆ๊ธฐ ๊ฐ’์„ ๊ฐ€์ง€๋Š” Dictionary ์ƒ์„ฑ
let initalizedDictionary: [String: String] = ["name": "kdgt", "gender": "female"]

// let์œผ๋กœ ์„ ์–ธํ•œ ๋ถˆ๋ณ€ Dictionary๋Š” ์ˆ˜์ • ๋ถˆ๊ฐ€ - ์ปดํŒŒ์ผ ์˜ค๋ฅ˜ ๋ฐœ์ƒ
//emptyDictionary["key"] = "value"
// name ํ‚ค์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ’์ด Dictionary์— ์กด์žฌํ•˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ
// ์ปดํŒŒ์ผ ์˜ค๋ฅ˜ ๋ฐœ์ƒ - ์˜ต์…”๋„ ํŒŒํŠธ์—์„œ ์ƒ์„ธํžˆ ๋‹ค๋ฃน๋‹ˆ๋‹ค/ name์— ๊ฐ’์ด ์กด์žฌํ•˜์ง€ ์•Š์„ ์ˆ˜๋„ ์žˆ๋‹ค๋Š” ๋ถˆํ™•์‹ค์„ฑ ๋•Œ๋ฌธ
//let someValue: String = initalizedDictionary["name"]



//MARK: - Set
// ๋นˆ Int Set ์ƒ์„ฑ
var integerSet: Set<Int> = Set<Int>()
integerSet.insert(1)
integerSet.insert(100)
integerSet.insert(99)
integerSet.insert(99)
integerSet.insert(99)

print(integerSet) // [100, 99, 1]
print(integerSet.contains(1)) // true
print(integerSet.contains(2)) // false
integerSet.remove(100)
integerSet.removeFirst()

print(integerSet.count) // 1
// Set๋Š” ์ง‘ํ•ฉ ์—ฐ์‚ฐ์— ๊ฝค ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค
let setA: Set<Int> = [1, 2, 3, 4, 5]
let setB: Set<Int> = [3, 4, 5, 6, 7]

// ํ•ฉ์ง‘ํ•ฉ
let union: Set<Int> = setA.union(setB)
print(union) // [2, 4, 5, 6, 7, 3, 1]
// ํ•ฉ์ง‘ํ•ฉ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ
let sortedUnion: [Int] = union.sorted()
print(sortedUnion) // [1, 2, 3, 4, 5, 6, 7]
// ๊ต์ง‘ํ•ฉ
let intersection: Set<Int> = setA.intersection(setB)
print(intersection) // [5, 3, 4]
// ์ฐจ์ง‘ํ•ฉ
let subtracting: Set<Int> = setA.subtracting(setB)
print(subtracting) // [2, 1]

 

์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” ํŒŒ์ด์ฌ๊ณผ ๋ฌธ๋ฒ•์ด ๋งŽ์ด ๋น„์Šทํ•˜๋‹ค. ์ฒด๊ฐ์œผ๋กœ๋Š” ์ž๋ฐ” + ํŒŒ์ด์ฌ ์ •๋„? ์ธ ๊ฒƒ ๊ฐ™๋‹ค.