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

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.