Quick actions

cmd+k|ctrl+k

Navigation

Languages

Aware of Struct Class

Snippet info

Language

Swift

Visibility

public

Author

awesome2023

Created

2023-11-28T02:04:26.699862Z

Updated

2023-11-28T02:04:26.699862Z

class Parent {
    var child:Child!
    
    init() {}
    
    deinit {
        print("Parent's deinit called")
    }
 }
 
 class Child {
    unowned var parent:Parent
    
    init(parent:Parent) {
        self.parent = parent
        
    }
    
    deinit {
        print("Child's deinit called")
    }
 }
 
 var parent:Parent? = Parent()                      // parent refcount: 1
 var child:Child? = Child.init(parent: parent!)     // parent refcount: 1, child refcount: 1
 parent?.child = child                              // parent refcount: 1, child refcount: 2
 /*
  do some work with child and parent
  */
 parent = nil                                       //parent refcount: 0, child refcount: 1
 child = nil                                        //child refcount: 0
INFO