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

Refactoring into a Model: Challenge task 2 of 3

I cannot see what is wrong with my code below when answering task 2... "Switch to the implementation file and implement a 'quotes' method which will allocate and initialize the _quotes instance variable only if it is 'nil'. Initialize the array with any three strings."

#import "Quote.h"

@implementation Quote

- (NSArray *) quotes {
    if (_quotes == nil){
        _quotes = [[NSArray alloc] initWithObjects:@"What ",
                         @"is ",
                         @"wrong?", nil];

    }
    return _quotes;
}

It keeps telling me "Bummer! Remember to allocate and initialize the instance variable '_quotes' and return it from the method named 'quotes'."

7 Answers

Amit Bijlani
STAFF
Amit Bijlani
Treehouse Guest Teacher

Your answer should work unless you deleted the rest of the implementation that had the randomQuote method and the @end?

The rest of the implementation is still there...

import "Quote.h"

@implementation Quote

  • (NSArray *) quotes { if (_quotes == nil){ _quotes = [[NSArray alloc] initWithObjects:@"What ", @"is ", @"wrong?", nil];

    } return _quotes; }

  • (NSString *) randomQuote {

    return @"Some Quote"; }

@end

Amit Bijlani
Amit Bijlani
Treehouse Guest Teacher

I just pasted that answer and it passed.

hmm, not sure why it hates me...

Sorry, I just learned about Markdown, below should be a better view of the code snippet

``` Objective C

import "Quote.h"

@implementation Quote

  • (NSArray *) quotes { if (_quotes == nil){ _quotes = [[NSArray alloc] initWithObjects:@"What ", @"is ", @"wrong?", nil];

    } return _quotes; }

  • (NSString *) randomQuote {

    return @"Some Quote"; }

@end

hopefully that is more readable. :)
Paul Yorde
Paul Yorde
10,497 Points

Hi James,

Are you certain you are writing the code to the correct file? It should be 'Quote.m.'. I pasted your code from above, and it works.

N B
N B
2,040 Points

The code is correct, for Quote.m Make sure the array property in challenge 1 (Quote.h) isn't set to readonly.

Michael McLaughlin
Michael McLaughlin
14,033 Points

I'm having this problem too. I tried both my answer, (same syntax and everything) in both chrome and firefox. what gives?

Michael McLaughlin
Michael McLaughlin
14,033 Points

ok, I think I figured it out, you can't forget to set the output data type to NSArray both on the .h and implementation file.