Swift Deinitialization
Deinitialization - Automatic Reference Counting, Deinitializers in Action
1. Deinitializer on Class Types π©βπ»
Deinitializer
λ class instance μ ν λΉμ΄ ν΄μ (deallocate)λκΈ° μ§μ μ νΈμΆ
λλ©°, deinit
keyword λ₯Ό μ΄μ©ν΄ μμ±νλ€.
Deinitializerλ class νμ
μμλ§ μ¬μ©λ μ μλ€.
μΌλ° μ½λ λΈλμ νμΆνκΈ° μ§μ μ νΈμΆλλ μ½λλ defer keyword λ₯Ό μ¬μ©ν΄ μ μνλ€.
2. How Deinitialization Works π©βπ»
Swift λ 리μμ€ ν보λ₯Ό μν΄ μλμΌλ‘ λ μ΄μ νμνμ§ μμ instances
λ₯Ό deallocate
νλ€. μ΄λ₯Ό μν΄ Swift λ
ARC
(Automatic Reference Counting)λ₯Ό μ΄μ©ν΄ instances μ λ©λͺ¨λ¦¬λ₯Ό κ΄λ¦¬νλ€.
μΌλ°μ μΌλ‘ instances μ deallocate λ₯Ό μλμΌλ‘ ν νμλ μλ€. νμ§λ§ μκΈ° μμ μ 리μμ€λ₯Ό μ΄μ©νλ κ²½μ° μ§μ cleanup μ
μν
ν΄μ€μΌνλ€. μλ₯Ό λ€μ΄, νμΌμ μ΄κ³ λ°μ΄ν°λ₯Ό μ°κΈ° μν΄ custom class
λ₯Ό μμ±νλ κ²½μ° class instance κ° deallocated λκΈ°
μ λ°λμ νμΌμ λ«μ 리μμ€λ₯Ό μ 리
ν΄μΌνλ€.
Syntax
deinit {
// perform the deinitialization
}
Deinitializer λ Class μ νλλ§ μ‘΄μ¬
νλ©°,νλΌλ―Έν°κ° μ‘΄μ¬νμ§ μμΌλ―λ‘
, λ³λμ κ΄νΈ μμ΄ μμ±νλ€.
Deinitializer λ instance κ° deallocation λκΈ° μ μλμΌλ‘ νΈμΆλλ©°,λͺ μμ μΌλ‘ νΈμΆν μ μλ€
.
Superclasses μ Deinitializer λ Subclasses μ μμ
λλ©°, Superclasses μ Deinitializer λSubclasses μ Deinitializer μ λ§μ§λ§μ μλμΌλ‘ νΈμΆ
λλ€. Superclasses μ Deinitializer λ Subclasses κ° μμ μ Deinitializer λ₯Ό μ 곡νμ§ μλλΌλ νμ νΈμΆλλ€.Class Instance λ Deinitializer κ° νΈμΆλκΈ° μ κΉμ§ deallocated λμ§ μκΈ° λλ¬Έμ,
Deinitializer λ instance μ λͺ¨λ Properties μ μ κ·Ό λ° μμ
ν μ μλ€.
3. Deinitializers in Action π©βπ»
Bank μ Player classes λ₯Ό μ΄μ©ν κ²μμ ν΅ν΄ Deinitializers λ₯Ό μ΄ν΄νλλ‘ νμ.
1. Bank class
Back class λ μ ν΅μ€μΈ μ½μΈμ΄ 10,000 κ°λ₯Ό λμ§ μλλ‘ μ‘°μ νλ€. κ²μμμ νλμ Bank λ§ μ‘΄μ¬
ν μ μκΈ° λλ¬Έμ, Bank λ
Class λ‘ κ΅¬ν
λλ©°, μ½μΈμ κ΄λ¦¬νκΈ° μν properties μ methods λ₯Ό κ°λλ€.
Bank λ coinsInBank
property λ₯Ό μ΄μ©ν΄ νμ¬ μ ν΅μ€μΈ μ½μΈμ κ°μλ₯Ό μΆμ νλ€.
λν μ½μΈμ λΆλ°°μ μμ§μ μ²λ¦¬νκΈ° μν΄ 2κ°μ methods distribute(coins:)
μ receive(coins:)
λ₯Ό μ 곡νλ€.
distribute(coins:)
λ μ½μΈμ λΆλ°°νκΈ° μ μνμ λ¨μ μ½μΈμ μλ₯Ό κ²μ¬ν΄ μν μκ³ λ³΄λ€ λ§μ μ½μΈμ μꡬν κ²½μ°,
λΆλ°° κ°λ₯ν λ¨μ μ½μΈ λ§νΌλ§ λΆλ°°νλ€.
class Bank {
static var coinsInBank = 10_000
static func distribute(coins numberOfCoinsRequested: Int) -> Int {
let numberOfCoinsToVend = min(numberOfCoinsRequested, coinsInBank)
coinsInBank -= numberOfCoinsToVend
return numberOfCoinsToVend
}
static func receive(coins: Int) {
coinsInBank += coins
}
}
2. Player class
Player class λ μ§κ°μ μ½μΈμ κ΄λ¦¬νκΈ° μν coinsInPurse
property λ₯Ό κ°μ§κ³ μμΌλ©°, μ΄κΈ°ν ν λ Bank λ‘λΆν°
μΌμ λμ μ½μΈμ λΆλ°° λ°λλ€. Player λ win(coins:)
methods λ₯Ό ν΅ν΄ μνμΌλ‘λΆν° μ½μΈμ νλνκ³ , κ²μμ κ·Έλ§λ λ
Deinitializer λ₯Ό ν΅ν΄ μμ ν λͺ¨λ μ½μΈμ μνμ λ°ννλ€.
class Player {
var coinsInPurse: Int
init(coins: Int) {
coinsInPurse = Bank.distribute(coins: coins)
}
func win(coins: Int) {
coinsInPurse += Bank.distribute(coins: coins)
}
deinit {
Bank.receive(coins: coinsInPurse)
}
}
3. Play Game and Deinitializers in Action
νλ μ΄μ΄λ μΈμ λ κ²μμ λ λ μ μκΈ° λλ¬Έμ Optional
λ‘ μ μΈνκ³ , ?
λλ !
λ₯Ό λΆμ¬ μ κ·Όνλ€.
var playerOne: Player? = Player(coins: 100)
print("A new player has joined the game with \(playerOne!.coinsInPurse) coins")
print("There are now \(Bank.coinsInBank) coins left in the bank")
A new player has joined the game with 100 coins
There are now 9900 coins left in the bank
playerOne?.win(coins: 2_000)
print("A new player has joined the game with \(playerOne?.coinsInPurse ?? 0) coins")
print("There are now \(Bank.coinsInBank) coins left in the bank")
A new player has joined the game with 2100 coins
There are now 7900 coins left in the bank
κ·Έλ¦¬κ³ νλ μ΄μ΄κ° μΆκ°λ‘ κ²μμ μ°Έκ°νλ€.
var playerTwo: Player? = Player(coins: 1000)
print("PlayerOne has joined the game with \(playerOne!.coinsInPurse) coins")
print("PlayerTwo has joined the game with \(playerTwo!.coinsInPurse) coins")
print("There are now \(Bank.coinsInBank) coins left in the bank")
PlayerOne has joined the game with 2100 coins
PlayerTwo has joined the game with 1000 coins
There are now 6900 coins left in the bank
playerOne μ΄ κ²μμ λ λλ€.
playerOne = nil
if playerOne != nil {
print("PlayerOne has joined the game with \(playerOne!.coinsInPurse) coins")
} else {
print("PlayerOne has left the game")
}
if playerTwo != nil {
print("PlayerTwo has joined the game with \(playerTwo!.coinsInPurse) coins")
} else {
print("PlayerTwo has left the game")
}
print("There are now \(Bank.coinsInBank) coins left in the bank")
PlayerOne has left the game
PlayerTwo has joined the game with 1000 coins
There are now 9000 coins left in the bank
νλ μ΄μ΄κ° κ²μμ λ λλ νμλ
Optional playerOne λ³μμ 'nil' μ ν λΉ
νλ κ²μΌλ‘ μ΄λ£¨μ΄μ§λ€.
playerOne μnil
μ ν λΉνλ€λ κ²μno Player instance λ₯Ό μλ―Έ
νλ©°,Player instance μ λν playerOne λ³μμ 'reference'κ° κΉ¨μ§λ€
.
μλ¬΄λ° Properties λλ Variables λ Player instance λ₯Ό μ°Έμ‘°νμ§ μλλ€λ©΄, λ©λͺ¨λ¦¬ 리μμ€ ν보λ₯Ό μν΄ instance μ λν μ°Έμ‘° ν λΉμ΄ ν΄μ λλ€
(itβs deallocated in order to free up its memory).μ΄λ¬ν μΌμ΄ λ°μλκΈ° μ§μ , Deinitializer κ° μλμΌλ‘ νΈμΆλλ©°, μ μνλλ‘ μμ ν λͺ¨λ μ½μΈμ μνμ λ°ννλ€.
playerTwo = nil
if playerOne != nil {
print("PlayerOne has joined the game with \(playerOne!.coinsInPurse) coins")
} else {
print("PlayerOne has left the game")
}
if playerTwo != nil {
print("PlayerTwo has joined the game with \(playerTwo!.coinsInPurse) coins")
} else {
print("PlayerTwo has left the game")
}
print("There are now \(Bank.coinsInBank) coins left in the bank")
PlayerOne has left the game
PlayerTwo has left the game
There are now 10000 coins left in the bank
playerTwo μμ κ²μμ λ λλ©° Deinitializer κ° νΈμΆ
λ¨μΌλ‘ μνμ λͺ¨λ μ½μΈμ΄ λ°νλλ€.
Reference
- βDeinitialization.β The Swift Programming Language Swift 5.7. accessed Dec. 19, 2022, Swift Docs Chapter 14 - Deinitialization.