Last active
August 17, 2018 15:57
-
-
Save kidap/51d78bf345764380fe85406736839843 to your computer and use it in GitHub Desktop.
XCode Playground - strong vs weak
This file contains hidden or 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 | |
| class Restaurant { | |
| var table: Table! | |
| deinit{ print("Restaurant deinitialized") } | |
| } | |
| class Table { | |
| weak var restaurant: Restaurant! //OPTION 1 | |
| //--------------------------- | |
| // OPTION 1: weak var restaurant: Restaurant! | |
| // both restaurant & table will be deinitialized | |
| //--------------------------- | |
| //--------------------------- | |
| // OPTION 2: var restaurant: Restaurant! | |
| // strong is implied | |
| // both restaurant & table will NOT be deinitialized | |
| // causing a retain cycle | |
| //--------------------------- | |
| deinit{ print("Table de-initialized") } | |
| } | |
| print("Start") | |
| do { | |
| let myRestaurant = Restaurant() | |
| let myTable = Table() | |
| myRestaurant.table = myTable | |
| myTable.restaurant = myRestaurant | |
| print("END") | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment