Actually it is not necessary to set the ivar to nil, since this is done automatically. For example if you did the following
Code:
Peg *peg1 = nil;
Peg *peg2;
NSLog(@"peg1 = %@ at address %p",peg1,peg1);
NSLog(@"peg2 = %@ at address %p",peg2,peg2);
The output would be
2012-11-07 08:08:21.977 TestNil[1133:c07] peg1 = (null) at address 0x0
2012-11-07 08:08:21.980 TestNil[1133:c07] peg2 = (null) at address 0x0
nil is the address 0x0. Messages can be safely sent to nil since they are just ignored.
Only when you alloc and init it do you get an address other than nil
Code:
peg1 = [[Peg alloc] init];
peg1.color = 'r';
NSLog(@"peg1 = %@ at address %p",peg1,peg1);
2012-11-07 08:08:21.981 TestNil[1133:c07] peg1 = <Peg: 0x743ff30> at address 0x743ff30
If a variable is a type, not an ivar, it is not safe to fail to initialize it. For example
Code:
int i;
int j = 0;
NSLog(@"i is %d at address %p",i,&i);
NSLog(@"j is %d at address %p",j,&j);
Both i and j are given an address in memory, and what ever was in that memory address before will provide a garbage value.
2012-11-07 08:08:21.982 TestNil[1133:c07] i is 6045295 at address 0xbfffdb64
2012-11-07 08:08:21.983 TestNil[1133:c07] j is 0 at address 0xbfffdb60
The value of i in this case is unpredictable.
Bob