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 Build a Simple iPhone App (iOS7) Refactoring into a Model Creating a Custom Class

Challenge Task 2 of 3 iOS Refactoring into a Model quiz...

This code is not working and I am not sure how to make it work...they gave us the randomQuote string, but asked us to allocate and initialize _quotes for nil and with three string answers...

Objective-C

import "Quote.h"

@implementation Quote

  • (NSString *) randomQuote {
  • (NSArray *) quotes { if (_quotes == nil) { _quotes = [[NSArray alloc] initWithObjects:@"What", @"Am I", @"doing?", nil]; } } return @"Some Quote"; }

@end __

3 Answers

Make sure you didn't write "readonly" in Quote.h. It works for me.

@property(strong,nonatomic, readonly) NSArray *predictions;

Here is my code for Quote.m:

#import "Quote.h"

@implementation Quote

- (NSArray *) quotes {
    if(_quotes == nil){
       _quotes = [[NSArray alloc]initWithObjects:@"What",@"Am I",@"doing?", nil];
    }
    return _quotes;
}

- (NSString *) randomQuote {
    return @"Some Quote";
}

@end

I hope I was able to help.

It looks like your "if "statement is in the wrong place. I think you want to start with the If condition then create the array depending on the result. In this case, if "_quotes" doesn't exist , then create it with alloc and initWithObjects.

"randomQuote" returns a string, so you will want to specify the string object in the array you want to return. The code below is returning "What". I commented it out because I'm not sure if thats what you want to do at this point.

I am guessing task 3 of 3 will have you implement "arc4random_uniform" in order to help you generate the random quote.

-(NSString *) randomQuote{

if(_quotes == nil){

    _quotes = [[NSArray alloc]initWithObjects:@"What",@"Am I",@"doing?", nil];
}

// return [_quotes objectAtIndex:0]; }

I hope I was able to help.

Aaron Batchelder
Aaron Batchelder
3,240 Points

I'm also having issues with this problem. I'm not sure what's going on with my code.

Quote.h

@interface Quote : NSObject

@property (strong, nonatomic) NSArray *quotes;

- (NSString *) randomQuote;

@ends 

#import "Quote.h"

@implementation Quote

- (NSArray*) quotes {
  if (_quotes) == nil) {
      _quotes = [[NSArray alloc] initWithObjects: @"Test", @"foo", @"bar", nil];
  }
    return _quotes;
}

- (NSString *) randomQuote {
    return @"Some Quote";
}

@end

Any help is appreciated!