π©π»π»/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]
μμ£Ό μ¬μ©νλ νμ΄μ¬κ³Ό λ¬Έλ²μ΄ λ§μ΄ λΉμ·νλ€. 체κ°μΌλ‘λ μλ° + νμ΄μ¬ μ λ? μΈ κ² κ°λ€.