Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
David Brown
1,152 PointsDesignated Initializers and Convenience Constructors
Hi, I have a quick question regarding convenience constructors.
Within the designated initialiser we access the init method from it's parent class 'NSObject' using self = [super init]
But when accessing the method alloc from it's parent class within the convenience constructor, return [self alloc]..., we don't say, return self = [super alloc]...
Why is that?
4 Answers
Pavel Palancica
6,535 PointsHey David,
Can you drop me a quick code snippet, and I'll give you cool answer :) ?
David Brown
1,152 PointsHi Pavel, Thanks in advance, code snippet below from blogPost.m file.
- (id) initWithTitle:(NSString *)blogTitle{
self = [super init];
if (self) {
[self setBlogTitle:blogTitle];
}
return self;
}
+ (id) blogPostWithTitle:(NSString *)blogTitle{
return [[self alloc] initWithTitle:blogTitle];
}
Pavel Palancica
6,535 PointsBasically alloc allocates the memory needed for the object. And init should initialize all its instance variables, or properties.
Because we write our custom initializer, it's normal to call self = [super init]; (in general, the super-class may be smth else than NSObject), and we have to make sure everything is initialized correctly (possible, in a more complicated case, there is a Database connection that is created in the super-class).
The alloc method actually does initialisation. It initialises the isa pointer to the correct class pointer, and initialises all iVars to 0, and objects to nil. And because we do not override it, probably the compiler automatically generates this method for us at run-time, and calls its super-class' alloc method on our behalf (not sure 100% if my affirmation here is correct, never thought about it, but I know in Java if we do not override the default constructor, the compiler generates smth similar to what I've just said).
I'm an iOS developer for about 2 years now, but never created my own alloc method so far, so we should just follow the common rules, and we should be very happy :D.
Just read smth about memory management, and you may find the exact answer: http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/MemoryMgmt.html#//apple_ref/doc/uid/10000011-SW1
Also, I see here: https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSObject_Class/Reference/Reference.html
it is said: "Do not override alloc to include initialization code. Instead, implement class-specific versions of init... methods.
For historical reasons, alloc invokes allocWithZone:. "
Hope it helped :)
Amit Bijlani
Treehouse Guest TeacherGreat answer Pavel Palancica !
Just to add to that. In this specific instance. The method: initWithTitle has to create a new instance of BlogPost and return it. However, the method blogPostWithTitle has to simply call the existing method initWithTitle. The former method exists in the class BlogPost itself and not the in the super class.
Another way to write the blogPostWithTitle method would be:
+ (id) blogPostWithTitle:(NSString *)blogTitle{
return [[BlogPost alloc] initWithTitle:blogTitle];
}