Are there any difference between following ways to init self?
First:
- (id)init {
self = [super init];
if (self) {
}
return self;
}
Second:
- (id)init {
if (self = [super init]) {
}
return self;
}
I like to use second way to init self. But I often see the first way when I create a new class by Xcode, it will generate those codes automatically. I am afraid the second way will make some errors in some condition which I don’t know.
Nope, they are the same.
The second method is just missing out the first assignment of
selfAnother way you might come across is this:
This puts the exceptional condition in the
ifstatement, and keeps the code that you expect to run normally (the Happy Path) closer to the left margin.