I\'m making another attempt today to try to understand retain cycles and weak references in Swift. Reading through the documentation, I saw the following code example where one
Your question doesn't provide enough information for us to answer it. You need to step back and study iOS memory management.
The core concept is object ownership. When you create an object and store a pointer to it in a strong variable, the system increments the retain count for that object. When the variable goes out of scope or you store nil into it, the system decrements the retain count. When the retain count drops to zero, the object is deallocated.
In order for an object to continue to live, you need to have at least one strong reference to it. If you don't it will be deallocated.
A weak pointer is NOT an owning reference.
If the only references to an object are weak references, it will be deallocated, possibly immediately. Weak references are special; the compiler zeros them out when the object is deallocated. This means that you won't crash if you try to send a message to an object saved in a weak variable. If it was deallocated, the pointer is changed to nil and the message is simply ignored.
As pointed out by @vacawama, sending messages to a nil object is the Objective-C way of doing things. (I've been working full-time for a client lately in Objective-C, so that tends to be my mindset lately. The question was about Swift however.)
In Swift, you use optional chaining instead, and syntax like this:
object?.method().
With this Swift syntax, if object is nil, the method call is skipped.
If you have 2 objects that each have weak references to each other, that's fine, but somewhere else in your program you need to have strong (owning) references to both objects or they will be deallocated.
If you have 2 objects that have strong references to each other, you've created a "retain cycle", and unless you nil one of those pointers at some time in the future, the objects will never be deallocated. If you have 2 (or more) objects that have strong references to each other but you don't have any other references to those objects, you've caused a memory leak.