There was a case when not explicitly initilizing a local CGFloat to 0 would result the variable holding garbage:
-(void)foo
{
CGFloat aFloat;
NSLog(@"float:%f", aFloat);
aFloat = 70;
}
[self foo];
[self foo];
Output:
float:0
float:70
So it really should print 0 both times, but since I didn’t explicitly initialize the float to 0, it contained garbage the second time around. My question is, does this apply to objects as well? Is there a difference for local variables between these two options:
1. NSObject *object;
2. NSObject *object = nil;
A pointer is initially nil if it is an ivar. (It is an ivar if you have declared it in the @interface part of your class.) If the pointer is a local variable (you declared it in a method) it will contain garbage. It is best practice to always assign something right away.
Update: As pointed out in the comments by omz, if you are using ARC, your pointers are also nilled if they are local variables.