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

iOS

Help 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
STAFF
Amit Bijlani
Treehouse Guest Teacher

A convenience constructor starts with name of the class which in turn can call a designated initializer. The designated initializer is: initWithSinger.

I'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
Amit Bijlani
Treehouse Guest Teacher

The 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.

Thanks, that makes better sense to me now!

I 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
Amit Bijlani
Treehouse Guest Teacher

The 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.

Thank you, Amit! This code challenge was a bit confusing :)

Was "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
Amit Bijlani
Treehouse Guest Teacher

The question does say:

Fill in the convenience constructor below for the class Song, which calls a designated initializer named: initWithSinger: (NSString*) singer

yeah this one was a bit tricky but totally makes sense!

The 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.