-
Mutability of Collections - CollectionsiOS/π Swift 2020. 12. 1. 02:18
"컬λ μ μ κ°λ³μ±"
Swift μ μ£Όμ 컬λ μ μ Arrays, Sets, Dictionaries μ λλ€.
μ΄λ€μ λμλ°©μμ λ€λ₯Έ νλ‘κ·Έλλ° μΈμ΄μ ν¬κ² λ€λ₯΄μ§ μκ³ , μμκ°λ₯ν©λλ€.
Swift μμ μ΄λ€μ κ°λ³μ±(Mutability) μ varμ ν λΉνλμ§ let μ ν λΉνλμ§μ λ°λΌ λ¬λΌμ§κ² λ©λλ€.
var μ ν λΉνλ©΄ mutable μ΄ λκ³ ,
element μ μΆκ°, μ κ±°, λ³κ²½μ΄ κ°λ₯ν΄μ§λλ€.
λ°λλ‘ let μ ν λΉνλ©΄ immutable μ΄ λκ³ ,
element μ μΆκ°, μ κ±°, λ³κ²½μ΄ λΆκ°λ₯ν΄μ§λλ€.
λ¨, element κ° μ°Έμ‘° νμ μΌ κ²½μ°, elementμ μν λ©€λ²μ κ°λ³μ±μλ μν₯μ μ£Όμ§ μμ΅λλ€.
κ΄λ ¨ κΈ: Value Type vs Refrence Type
νν κ°νμ κ³Ό μ°Έμ‘°νμ μ λΉκ΅ μ€λͺ ν λ struct μ classλ₯Ό μλ‘ λ€κ³€ ν©λλ€.
λ°λΌμ μ΄ λμ μ΄μ©ν μμ μ½λλ₯Ό λ§λ€μ΄ 보μμ΅λλ€.
class Person { var name: String init(_ name: String) { self.name = name } } struct Animal { var type: String // Memberwise initializer κ° μλ μμ±λλ―λ‘ λ°λ‘ μμ±μλ₯Ό λ§λ€μ§ μμμ΅λλ€. } let personList: [Person] = [Person("Park"), Person("Kim")] let animalList: [Animal] = [Animal(type: "Cat"), Animal(type: "Dog")] personList[0].name = "Lee" // μ°Έμ‘° νμ μΈ class μ κ°λ³μ±μ λ©€λ²μ κ°λ³μ±μ μν₯μ μ£Όμ§ μμ΅λλ€. animalList[0].type = "Elephant" // Complie Error - κ° νμ μΈ structμ κ°λ³μ±μ λ©€λ²μ κ°λ³μ±μ μν₯μ μ€λλ€. personList.removeFirst() // Complie Error - λ¬Όλ‘ element μ μΆκ°, μμ , λ³κ²½μ λΆκ°λ₯ν©λλ€.
μμ κ°μ λΆλΆμ΄ μ κ° collection μ κ°λ³μ±μ μ΄ν΄νλλ° νΌλμ μ£Όμμ΅λλ€.
μ΄λ² ν¬μ€νΈλ₯Ό μ°λ©΄μ λͺ λ£νκ² μ 리λ λΆλΆμ λλ€.
λ.
μ°Έκ³ μλ£ : docs.swift.org/swift-book/LanguageGuide/CollectionTypes.html
Collection Types — The Swift Programming Language (Swift 5.3)
Collection Types Swift provides three primary collection types, known as arrays, sets, and dictionaries, for storing collections of values. Arrays are ordered collections of values. Sets are unordered collections of unique values. Dictionaries are unordere
docs.swift.org
μ΄μ κΈ: Self Type - General
Self Type - General
" μ»΄νμΌλ¬κ° 컨ν μ€νΈμ μν΄ μΆλ‘ νλ νμ " *Self Type μ νμ μ λλ€. μμ± self μλ λ€λ₯Έ κ°λ μ λλ€. extension Int { static let zero: Self = 0 // Int λ‘ μΆλ‘ λ©λλ€. var zero: Self { // Int λ‘ μΆ..
woozzang.tistory.com
'iOS > π Swift' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
ARC - Memory (0) 2020.12.30 [Swift] κ°νμ κ³Ό μ°Έμ‘°νμ ( Value Type vs Reference Type ) (4) 2020.12.29 Self Type - General (0) 2020.11.27 [Swift] Property Observer (μμ± κ°μμ) λ? (0) 2020.11.27 Side Effect - General (0) 2020.11.24