Seems like whenever we need to initialize an object, we need to immediately write
-(id) init {
self = [super init];
if (self) {
// initialize values here
}
return self;
}
is there a shorter way to do it? Maybe even by a macro?
You can get away with one line fewer, but this isn’t recommended practice anymore:
Boilerplate is boilerplate, unfortunately, and each of the things that initializers do: assign
selfto the result of[super init](or the superclass’s designated initializer), checkselfis notnil, initialize ivars, and returnself(whethernilor a valid object) are all necessary.To spare yourself some typing, you can make a code snippet in Xcode.