Whats the difference here (or, when to alloc init)


#1

I’m doing the bronze challenge and my code differs from how you do it in the book:

book:

UIImage *i = [UIImage imageNamed:@"Hypno.png"]; self.tabBarItem.image = i;

me:

Why use one instead of the other? When is it good to alloc and init the object pointer, and when can I just do I like I did?

by the way, is there a difference in memory handling or anything like retaining or releasing stuff?

Thanks!


#2

Keep in mind that in both cases, you are allocating a UIImage. The only difference is that in the first example, you are storing a reference to that allocating UIImage in a pointer named i.

A lot of the time, including in the book and in the specific example you gave, we separate the lines for readability. Especially when lines get really long, it can be nice to separate them into multiple lines. At the end of the day, there is no difference between the two code listings you provided (the compiler actually optimizes out “unnecessary” variables, and so the compiled code would be the same in both circumstances.)


#3

Thanks!! It clears things up a lot for me!!