Skip to content

Instantly share code, notes, and snippets.

@danielprast
Created December 6, 2022 01:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielprast/e67b5dc5355802df825d4b889a44a1c7 to your computer and use it in GitHub Desktop.
Save danielprast/e67b5dc5355802df825d4b889a44a1c7 to your computer and use it in GitHub Desktop.
Sample case of Retain Cycle in Swift
import Foundation
// MARK: - Swift Memory Management
class Tutorial {
let title: String
unowned let author: Author
weak var editor: Editor?
init(title: String, author: Author) {
self.title = title
self.author = author
}
deinit {
print("Sampai jumpa lagi tutorial \(title)!")
}
}
class Editor {
let name: String
var tutorials: [Tutorial] = []
init(name: String) {
self.name = name
}
deinit {
print("Sampai jumpa lagi editor \(name)!")
}
}
class Author {
let name: String
var tutorials: [Tutorial] = []
init(name: String) {
self.name = name
}
deinit {
print("Sampai jumpa lagi author \(name)!")
}
}
do {
let author = Author(name: "Martin")
let tutorial = Tutorial(
title: "Swift Memory Management",
author: author
)
let editor = Editor(name: "Joko")
author.tutorials.append(tutorial)
tutorial.editor = editor
editor.tutorials.append(tutorial)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment