I'm from the C++ world so the notion of assigning this
makes me shudder:
this = new Object; // Gah!
But in Objective-C there is a similar keyword, self
, for which this is perfectly acceptable:
self = [super init]; // wait, what?
A lot of sample Objective-C code uses the above line in init
routines. My questions:
1) Why does assignment to self
make sense (answers like "because the language allows it" don't count)
2) What happens if I don't assign self
in my init
routine? Am I putting my instance in some kind of jeopardy?
3) When the following if
statement fails, what does it mean and what should I do to recover from it:
- (id) init
{
self = [super init];
if (self)
{
self.my_foo = 42;
}
return self;
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…