Created
December 6, 2022 01:26
-
-
Save danielprast/e67b5dc5355802df825d4b889a44a1c7 to your computer and use it in GitHub Desktop.
Sample case of Retain Cycle in Swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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