Skip to content

Instantly share code, notes, and snippets.

@alexdremov
Created January 9, 2023 09:40
Show Gist options
  • Save alexdremov/0c938dea99b59b5ab1f2b6aa163069d1 to your computer and use it in GitHub Desktop.
Save alexdremov/0c938dea99b59b5ab1f2b6aa163069d1 to your computer and use it in GitHub Desktop.
Swift uses ARC to track and deallocate unused objects. Learn about the three types of reference counts and how ARC works — in this detailed post. Post: https://alexdremov.me/dive-into-swifts-memory-management/
class Person {
let name: String
init(name: String) { self.name = name }
var apartment: Apartment?
deinit { print("\(name) is being deinitialized") }
}
class Apartment {
let unit: String
init(unit: String) { self.unit = unit }
weak var tenant: Person?
deinit { print("Apartment \(unit) is being deinitialized") }
}
var john: Person? = Person(name: "John Appleseed")
var unit4A: Apartment? = Apartment(unit: "4A")
john!.apartment = unit4A // Person -> Apartment: strong reference
unit4A!.tenant = john // Apartment -> Person: weak reference
john = nil
unit4A = nil
class Customer {
let name: String
var card: CreditCard?
init(name: String) {
self.name = name
}
deinit { print("\(name) is being deinitialized") }
}
class CreditCard {
let number: UInt64
unowned let customer: Customer
init(number: UInt64, customer: Customer) {
self.number = number
self.customer = customer
}
deinit { print("Card #\(number) is being deinitialized") }
}
var john: Customer? = Customer(name: "John Appleseed")
john!.card = CreditCard(number: 1234_5678_9012_3456, customer: john!)
john = nil // No retain cycle, both objects are deallocated
class User {
var id: Int
var name: String
init(id: Int, name: String) {
self.id = id
self.name = name
}
}
let user = User(id: 0, name: "John")
class Person {
let name: String
init(name: String) { self.name = name }
var apartment: Apartment?
deinit { print("\(name) is being deinitialized") }
}
class Apartment {
let unit: String
init(unit: String) { self.unit = unit }
var tenant: Person?
deinit { print("Apartment \(unit) is being deinitialized") }
}
var john: Person? = Person(name: "John Appleseed")
var unit4A: Apartment? = Apartment(unit: "4A")
john!.apartment = unit4A // Person -> Apartment: strong reference
unit4A!.tenant = john // Apartment -> Person: strong reference
john = nil // Person is no longer needed
unit4A = nil // Apartment is no longer needed
class Person {
var name: String
var voice: Voice? = nil
init(name: String) {
self.name = name
}
func say() { voice?.say() }
deinit {
print("Person deallocated")
}
}
class Voice {
var say: () -> ()
init(say: @escaping () -> ()) { self.say = say }
deinit {
print("Voice deallocated")
}
}
var person: Person? = Person(name: "Alex")
person!.say()
person = nil
self.voice = Voice {
print("My name is \(self.name)")
}
self.voice = Voice {[weak self] in
guard let self = self else { return; }
print("My name is \(self.name)")
}
My name is Alex
Person deallocated
Voice deallocated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment