Skip to content

Instantly share code, notes, and snippets.

@kidap
Last active August 17, 2018 15:57
Show Gist options
  • Select an option

  • Save kidap/51d78bf345764380fe85406736839843 to your computer and use it in GitHub Desktop.

Select an option

Save kidap/51d78bf345764380fe85406736839843 to your computer and use it in GitHub Desktop.
XCode Playground - strong vs weak
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