I was reading Apple's documentation about Objective-C's reference cycles and then I tried to create one, but I can't quite understand its behaviour. Here what I have: there are two classes XYZPerson
and XYZPersonSpouse
.
They have properties for their first name, last name and properties of type NSString
called spouseName
. In main
I set spouseName
properties of both classes to each others name like this (in init
of both classes I call their designated initializers
, which set their first names and last names):
XYZPerson *person = [[XYZPerson alloc] init];
XYZPersonSpouse *spouseOfXYZPerson = [[XYZPersonSpouse alloc] init];
spouseOfXYZPerson.spouseName = person.firstName;
person.spouseName = spouseOfXYZPerson.firstName;
I also override dealloc
method of both classes to print some text on the console. Now, because I don't use weak
or unsafe_unretained
, while defining properties spouseName
on both classes, I assume that by the code above I created a strong reference cycle. However, when later I assign another NSString
as a name of XYZPerson
class's instance person
like this:
person.spouseName = @"Julia";
(but even without this) and run my project, I keep seeing the message for the dealloc
method of XYZPersonSpouse
class (and for XYZPerson
, too).
Shouldn't the classes still not be kept because of the reference cycle? If you could explain what is going on here, I would appreciate your help.