This isn't guaranteed. Releases may be optimized to happen earlier than this, to any point after the last formal use of the strong reference. Since the strong reference loaded in order to evaluate the left-hand side weakProperty?.variable
is not used afterward, there is nothing keeping it alive, so it could be immediately released. If there are any side effects in the getter for variable
that cause the object referenced by weakProperty
to be deallocated, nil-ing out the weak reference, then that would cause the force-unwrap on the right side to fail. You should use if let
to test the weak reference, and reference the strong reference bound by the if let
:
if let property = weakProperty {
property.variable = property.otherVariable
property.performMethod(property)
}
This should safer and also more efficient, since the weak reference is loaded and tested once instead of four times.