Sobes.tech
Junior — Senior

What will be output in example №7

livecode

Task condition

In this example, two classes are created, each holding a weak reference to an object of the other class. After creating instances and setting mutual delegates, the objects and their delegates are printed to understand what values will be displayed.

import Foundation

class A: NSObject {
    weak var delegate: B?
}

class B: NSObject {
    weak var delegate: A?
}

var a: A?
var b: B?

func configure() {
    a = A()
    b = B()

    a?.delegate = b
    b?.delegate = a
}

configure()

print(a)              // ?
print(b)              // ?
print(a?.delegate)    // ?
print(b?.delegate)    // ?