Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS

Using a custom class quiz

Having a spot of bother with these 3 questions

1) Create an instance of the Song with alloc and init Song *popSong = ______________;

2) Create an instance of the Song class with the convenience constructor songWithSinger:(NSString *)s and singer named "James Brown". Song *popSong = ____________;

3) Create an instance of the Song class with the designated initializer initWithSinger:(NSString *)s and singer named "James Brown". Song *popSong = ______________;

If someone could help me out and quickly explain how it works that would be great, gone back over some videos and still struggling :<

Thanks in advance

4 Answers

Thomas Nilsen
Thomas Nilsen
14,957 Points

1) Song *popSong = [[Song alloc] init];

2) Song *popSong = [Song songWithSinger:@"James Brown"];

3) Song *popSong = [[Song alloc] initWithSinger:@"James Brown"];

Meaning the convenience method would look something like this:

+ (id) songWithSinger:(NSString *)singer{
    return [[self alloc] initWithSinger:singer];
}

Designated method:

- (id) initWithSinger:(NSString *)singer{
    self = [super init];
    if(self){
        self.singer = singer;
    }
    return self;
}

I've seen what I've done wrong now... syntax. :@ so anoying lol

Thomas Nilsen
Thomas Nilsen
14,957 Points

I usually type the challenges out in Xcode, and then past it in :)

I think im going to start doing that aswell, the mistake i was making is that I was writing song instead of Song, little things!

For some reason this quiz does not accept a space between the ':' & '@'. Which, is valid syntax.. and hasn't been an issue on previous quizzes..

Both of the following SHOULD pass, but only the first actually does.

Song *popSong = [Song songWithSinger:@"James Brown"];
Song *popSong = [Song songWithSinger: @"James Brown"]; // Note the space
Amit Bijlani
Amit Bijlani
Treehouse Guest Teacher

Sorry about that. Our quiz engine is a bit restrictive. It's on our todo list to change these quizzes to code challenges.

Understandable, just wanted to post it so that if anyone else was pulling their hair out they could potentially find out why. Thanks for the course, great job Amit!