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
Chris Stanely
2,325 PointsHelp with question 4 on iOS Designated Initializers and Convenience Constructors
Fill in the convenience constructor below for the class Song, which calls a designated initializer named: initWithSinger: (NSString*) singer
+ ( id ) ????? : (NSString*) singer {
return [[ ?????? alloc]
initWithSinger: ?????? ];
}
I've put
+ ( id ) initWithSinger : (NSString*) singer {
return [[ self alloc] initWithSinger: singer ];
}
5 Answers
Amit Bijlani
Treehouse Guest TeacherA convenience constructor starts with name of the class which in turn can call a designated initializer. The designated initializer is: initWithSinger.
Brian Chewning
7,270 PointsI can not get this. I have tried
+ ( id ) initWithSinger : (NSString*) singer {
return [[ self alloc] initWithSinger: singer ];
+ ( id ) initWithSong : (NSString*) singer {
return [[ self alloc] initWithSinger: singer ];
+ ( id ) blogPostWithSinger : (NSString*) singer {
return [[ self alloc] initWithSinger: singer ];
+ ( id ) blogPostWithSong : (NSString*) singer {
return [[ self alloc] initWithSinger: singer ];
and nothing has worked. I am truly stumped.
Amit Bijlani
Treehouse Guest TeacherThe quiz makes no reference to a blog post. So why does your answer mention blog post? It should start with the name of the class which is Song.
Christian Georg Strobl
27,656 PointsThank you, Amit! This code challenge was a bit confusing :)
Ryan Sommers
1,528 PointsWas "songWithSinger" implied somehow, even though it's not mentioned in the question? Took awhile to get this one, especially since you had to get all (4 out of 4) the questions right.
Amit Bijlani
Treehouse Guest TeacherThe question does say:
Fill in the convenience constructor below for the class Song, which calls a designated initializer named: initWithSinger: (NSString*) singer
miles fishman
Courses Plus Student 3,610 Pointsyeah this one was a bit tricky but totally makes sense!
Brandon Ross
1,971 PointsThe two keys were "convenience constructor" and "initWithSinger".
The convenience constructor dictates that you must use the method name (which is not mentioned in this question), but can be inferred from the "initWithSinger" initializer and the class name, "Song". Ergo: songWithSinger must be the method name.
Jason Jameson
1,881 PointsJason Jameson
1,881 PointsI'm really confused as to why Chris' answer:
( id ) initWithSinger : (NSString*) singer {
return [[ self alloc] initWithSinger: singer ];
}
Is not considered the correct one. Would you please elaborate? I am not able to pass the quiz because of this question.
Amit Bijlani
Treehouse Guest TeacherAmit Bijlani
Treehouse Guest TeacherThe quiz refers to a convenience constructor which is method that handles the allocation and initialization. Whereas a designated initializer is a way to initialize an allocated object.
Convenience constructor: a method that starts with the name of the class which in this case would be a
songWithSinger.Designated initializer: a method that initializes an object and starts with the word
initWithSinger.Jason Jameson
1,881 PointsJason Jameson
1,881 PointsThanks, that makes better sense to me now!