Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-C creating object first then assign to property

Tags:

objective-c

Sorry if the title is confused but I can't find a way to express my question.

I see many Objective-C example codes and they usually use this:

    Clazz *clazz = [[Clazz alloc] init];
    self.clazz = clazz;

instead of:

    self.clazz = [[Clazz alloc] init];

Does the first approach have any advantage over the second one?

like image 945
kientux Avatar asked Dec 13 '25 05:12

kientux


1 Answers

There's no advantage if that's all it does. If the code uses clazz further, then accessing a local variable saves the nanoseconds required to call the self.clazz getter, and avoids the getter entirely which could matter if the getter does anything weird.

like image 119
rob mayoff Avatar answered Dec 16 '25 21:12

rob mayoff



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!