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 trialgregdodd
8,124 PointsDesignated initializers
So first we created the
- (id) initWithTitle:(NSString *)title;
to set our title with each new Blog Post class. Does adding the
+ (id) blogPostWithTitle: (NSString *)title;
not replace this now, or am I missing something.
Thanks
4 Answers
Stone Preston
42,016 PointsinitWithTitle is an initializer, whereas blogPostWithTitle is a convenience constructor. You can use either one, although using a convenience constructor is easier since you dont have to call alloc first.
[[BlogPost alloc] initWithTitle:@"Some title"];
[BlogPost blogPostWithTitle:@"some title"];
gregdodd
8,124 PointsSo if this is what I have in my BlogPost.m class
- (id) initWithTitle:(NSString *)title{
self = [super init];
if (self){
self.title = title;
self.author = nil;
self.thumbNail = nil;
}
return self;
}
+ (id) blogPostWithTitle:(NSString *)title {
return [[self alloc] initWithTitle:title];
}
I'm safe to remove the - (id) initWithTitle because they are doing the same thing?
Stone Preston
42,016 PointsNo, you cannot remove initWithTitle because your blogPostWithTitle constructor calls the initWithTitle method as seen below
+ (id) blogPostWithTitle:(NSString *)title {
return [[self alloc] initWithTitle:title];
}
if you removed it, blogPostWithTitle will not work
gregdodd
8,124 PointsDon't know how I overlooked that one, thanks! So in this case, what role is the initWithTitle playing? Could I change initWithTitle:title to initWithString: title?
Thanks for the quick responses btw, don't like to move ahead until I've got everything down.