Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

tranvutuan

macrumors member
Original poster
Dec 19, 2011
74
0
I am used to have this in my code
myClass.h
Code:
@property ( strong, nonatomic ) ClassA *varA

myClass.m
Code:
[COLOR="Blue"]varA  = [[ClassA alloc] init];[/COLOR]
if ( varA isEqual:nil )
   NSLog@"var A is nil");
else
   NSLog@"var A is not nil");

this is a result from console
Code:
var A is not nil
I thought var A should be nil then. So my question is what does blue line do and why var A is not nil after all
 
I suggest you read a beginners Objective-C book. Your question is extremely basic.

The alloc method of ClassA allocates memory for an instantiated object of the class. At that point, varA, which is really just a pointer, would have a value if all you did was this line in your implementation source.
Code:
varA = [ClassA alloc];

The only reason varA wouldn't have a value other than zero, would be if the alloc failed.

The init method, called against the instantiated object, is used to setup variables inside the new object. Given this information, you can follow that line above to this.
Code:
[varA init];

The init method could fail and return zero so it is safer to do this. There has been discussion of this fact online over the years.
Code:
varA = [varA init];
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.