Initialization
Initialization is the process of preparing an instance of a class, structure, or enumeration for use. This process involves setting an initial value for each stored property on that instance and performing any other setup or initialization that is required before the new instance is ready for use.
Setting Initial Values for Stored Properties
Classes and structures must set all of their stored properties to an appropriate initial value by the time an instance of that class or structure is created. Stored properties cannot be left in an indeterminate state.
You can set an initial value for a stored property within an initializer, or by assigning a default property value as part of the property’s definition. These actions are described in the following sections.
νλ‘νΌν° μ΄κΈ°κ°
- μ€μννΈμ λͺ¨λ μΈμ€ν΄μ€λ μ΄κΈ°νμ λμμ λͺ¨λ νλ‘νΌν°μ μ ν¨ν κ°μ΄ ν λΉλμ΄ μμ΄μΌ ν©λλ€.
- νλ‘νΌν°μ 미리 κΈ°λ³Έκ°μ ν λΉν΄λλ©΄ μΈμ€ν΄μ€κ° μμ±λ¨κ³Ό λμμ μ΄κΈ°κ°μ μ§λκ² λ©λλ€.
class PersonA {
// λͺ¨λ μ μ₯ νλ‘νΌν°μ κΈ°λ³Έκ° ν λΉ
var name: String = "unknown"
var age: Int = 0
var nickName: String = "nick"
}
// μΈμ€ν΄μ€ μμ±
let jason: PersonA = PersonA()
// κΈ°λ³Έκ°μ΄ μΈμ€ν΄μ€κ° μ§λ
μΌ ν κ°κ³Ό λ§μ§ μλ€λ©΄ μμ±λ μΈμ€ν΄μ€μ νλ‘νΌν°μ κ°κ° κ° ν λΉ
jason.name = "jason"
jason.age = 30
jason.nickName = "j"
μ΄λμ λΌμ΄μ (initializer)
- νλ‘νΌν° μ΄κΈ°κ°μ μ§μ νκΈ° μ΄λ €μ΄ κ²½μ°μλ μ΄λμ λΌμ΄μ initμ ν΅ν΄ μΈμ€ν΄μ€κ° κ°μ ΈμΌ ν μ΄κΈ°κ°μ μ λ¬ν μ μμ΅λλ€.
class PersonB {
var name: String
var age: Int
var nickName: String
// μ΄λμ
λΌμ΄μ
init(name: String, age: Int, nickName: String) {
self.name = name
self.age = age
self.nickName = nickName
}
}
let hana: PersonB = PersonB(name: "hana", age: 20, nickName: "νλ")
- νλ‘νΌν°μ μ΄κΈ°κ°μ΄ κΌ νμ μμ λλ μ΅μ λμ μ¬μ©
class PersonC {
var name: String
var age: Int
var nickName: String?
init(name: String, age: Int, nickName: String) {
self.name = name
self.age = age
self.nickName = nickName
}
// μμ λμΌν κΈ°λ₯ μν
// convenience init(name: String, age: Int, nickName: String) {
// init(name: name, age: age)
// self.nickName = nickName
// }
init(name: String, age: Int) {
self.name = name
self.age = age
}
}
let jenny: PersonC = PersonC(name: "jenny", age: 10)
let mike: PersonC = PersonC(name: "mike", age: 15, nickName: "m")
- μμμ μΆμΆ μ΅μ λμ μΈμ€ν΄μ€ μ¬μ©μ κΌ νμνμ§λ§ μ΄κΈ°κ°μ ν λΉνμ§ μκ³ μ ν λ μ¬μ©
class Puppy {
var name: String
var owner: PersonC!
init(name: String) {
self.name = name
}
func goOut() {
print("\(name)κ° μ£ΌμΈ \(owner.name)μ μ°μ±
μ ν©λλ€")
}
}
let happy: Puppy = Puppy(name: "happy")
//happy.goOut() // μ£ΌμΈμ΄ μλ μνλΌ μ€λ₯ λ°μ!!
μ€ν¨κ°λ₯ν μ΄λμ λΌμ΄μ
- μ΄λμ λΌμ΄μ 맀κ°λ³μλ‘ μ λ¬λλ μ΄κΈ°κ°μ΄ μλͺ»λ κ²½μ° μΈμ€ν΄μ€ μμ±μ μ€ν¨ν μ μμ΅λλ€.
- μΈμ€ν΄μ€ μμ±μ μ€ν¨νλ©΄ nilμ λ°ν
- μ€ν¨κ°λ₯ν μ΄λμ λΌμ΄μ μ λ°ννμ μ μ΅μ λ νμ μ λλ€.
- init?μ μ¬μ©
class PersonD {
var name: String
var age: Int
var nickName: String?
init?(name: String, age: Int) {
if (0...120).contains(age) == false {
return nil
}
if name.characters.count == 0 {
return nil
}
self.name = name
self.age = age
}
}
//let john: PersonD = PersonD(name: "john", age: 23)
let john: PersonD? = PersonD(name: "john", age: 23)
let joker: PersonD? = PersonD(name: "joker", age: 123)
let batman: PersonD? = PersonD(name: "", age: 10)
print(joker) // nil
print(batman) // nil
Deinitialization
A deinitializer is called immediately before a class instance is deallocated. You write deinitializers with the deinit keyword, similar to how initializers are written with the init keyword. Deinitializers are only available on class types.
λμ΄λμ λΌμ΄μ (deinitializer)
- deinitμ ν΄λμ€μ μΈμ€ν΄μ€κ° λ©λͺ¨λ¦¬μμ ν΄μ λλ μμ μ νΈμΆ
- μΈμ€ν΄μ€κ° ν΄μ λλ μμ μ ν΄μΌν μΌμ ꡬνν μ μμ΅λλ€.
- deinitμ 맀κ°λ³μλ₯Ό μ§λ μ μμ΅λλ€.
- μλμΌλ‘ νΈμΆλλ―λ‘ μ§μ νΈμΆν μ μμ΅λλ€.
- λμ΄λμ λΌμ΄μ λ ν΄λμ€ νμ μλ§ κ΅¬νν μ μμ΅λλ€.
- μΈμ€ν΄μ€κ° λ©λͺ¨λ¦¬μμ ν΄μ λλ μμ μ ARC(Automatic Reference Counting) μ κ·μΉμ λ°λΌ κ²°μ λ©λλ€.
class PersonE {
var name: String
var pet: Puppy?
var child: PersonC
init(name: String, child: PersonC) {
self.name = name
self.child = child
}
// μΈμ€ν΄μ€κ° λ©λͺ¨λ¦¬μμ ν΄μ λλ μμ μ μλ νΈμΆ
deinit {
if let petName = pet?.name {
print("\(name)κ° \(child.name)μκ² \(petName)λ₯Ό μΈλν©λλ€")
self.pet?.owner = child
}
}
}
var donald: PersonE? = PersonE(name: "donald", child: jenny)
donald?.pet = happy
donald = nil // donald μΈμ€ν΄μ€κ° λμ΄μ νμμμΌλ―λ‘ λ©λͺ¨λ¦¬μμ ν΄μ , deinit μ€ν
// "donaldκ° jennyμκ² happyλ₯Ό μΈλν©λλ€"
'iOS π > Swift' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
Swift15 - Nil-Coalescing Operator (0) | 2021.02.03 |
---|---|
Swift14 - Optional Chaining (0) | 2021.02.03 |
Swift12 - μμ (0) | 2021.02.02 |
Swift11 - νλ‘νΌν° (0) | 2021.02.02 |
Swift10 - first class citizen (0) | 2021.02.02 |