I am new to coding and trying to get up to speed with Objective-C. Came across some code I did not understand. I was hoping someone could clarify it for me. In the case below, I am not sure how *foo2 is working and why it is not being released?
ClassOne *pointer = [[ClassOne alloc]init];
ClassTwo *foo = [[ClassTwo alloc]init], *foo2;
foo2 = [foo add: pointer];
[foo release];
foo = foo2
[pointer release];
[foo release];
The variable
pointer
is a pointer to an object of class ClassOne. It it assigned the value of a newly created object.*foo
and*foo2
are objects of classClassTwo
. Onlyfoo
is assigned a newly created object.foo2
may point to anything, so it is unsafe to use it before assigning it a value.foo2
is assigned a value: I assume that theadd:
message of classClassTwo
creates an object (the signature of the method should be-(ClassTwo*)add:(ClassOne*);
)The object pointed to by
foo
is no longer needed.The variable
foo
is assigned the value offoo2
: both point to the same object.The object pointed to by
pointer
is no longer needed.The object pointed to by
foo
(and also byfoo2
) is no longer needed.