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

Caleb Kleveter
MOD
Caleb Kleveter
Treehouse Moderator 37,862 Points

I've used this code before, why won't it work?

I've done this code challenge before but I had to stop part-way though, the code below is the same (as far as I can tell) but it won't work, what's wrong?

Quote.h
@interface Quote : NSObject

// Add property here
@property (strong, nonatomic, readonly) NSArry *quotes;
- (NSString *) randomQuote;
@end
Quote.m
#import "Quote.h"

@implementation Quote

- (NSString *) randomQuote {

    return @"Some Quote";
}

@end
Carter Randall
Carter Randall
3,311 Points

You are missing an 'a' in NSArray where you are declaring the property.

Caleb Kleveter
Caleb Kleveter
Treehouse Moderator 37,862 Points

Thanks so much, can hardly believe I missed it :)

4 Answers

Stone Preston
Stone Preston
42,016 Points

your code is almost correct, with a few minor issues (in addition to the one Carter pointed out):

you added your code to the randomQuote method, you are supposed to implement a new method. called quotes that returns an NSArray.) .

I used the code below and passed the first 2 tasks just fine

#import "Quote.h"

@implementation Quote

- (NSArray *) quotes {
if (_quotes == nil){
   _quotes = [[NSArray alloc] initWithObjects:@"Happy, Happy, Happy", 
   @"Your gullible", @"Say cheese", nil]; 
    }
    return _quotes;
}


- (NSString *) randomQuote {

    return @"Some Quote";
}

@end

and remove the read only attribute from your property. it messes up the challenge and wont accept it with it in there:

@property (strong, nonatomic) NSArray *quotes;
Caleb Kleveter
MOD
Caleb Kleveter
Treehouse Moderator 37,862 Points

Still isn't working, I even tried to copy and paste the code and had no luck with it.

Stone Preston
Stone Preston
42,016 Points

are you sure ? I just did again and passed tasks 1 and 2 using the code above. below are the full .h and .m files:

@interface Quote : NSObject

// Add property here
@property (strong, nonatomic) NSArray *quotes;
- (NSString *) randomQuote;

@end
#import "Quote.h"

@implementation Quote

- (NSArray *) quotes {
if (_quotes == nil){
   _quotes = [[NSArray alloc] initWithObjects:@"Happy, Happy, Happy", 
   @"Your gullible", @"Say cheese", nil]; 
    }
    return _quotes;
}


- (NSString *) randomQuote {

    return @"Some Quote";
}

@end
Stone Preston
Stone Preston
42,016 Points

post what you currently have implemented

Caleb Kleveter
MOD
Caleb Kleveter
Treehouse Moderator 37,862 Points
@interface Quote : NSObject

// Add property here
@property (strong, nonatomic) NSArray *quotes;
- (NSString *) randomQuote;

@end
#import "Quote.h"

@implementation Quote

- (NSString *) randomQuote {
if (_quotes == nil){
   _quotes = [[NSArray alloc] initWithObjects:@"Happy, Happy, Happy", 
   @"Your gullible", @"Say cheese", nil]; 
    }
    return _quotes;
}


- (NSString *) randomQuote {

    return @"Some Quote";
}   

@end
Stone Preston
Stone Preston
42,016 Points

you have randomQuote implemented twice. you need to have a randomQuote method and a quotes method:

here is what you have:

#import "Quote.h"

@implementation Quote

//randomQuote once
- (NSString *) randomQuote {
if (_quotes == nil){
   _quotes = [[NSArray alloc] initWithObjects:@"Happy, Happy, Happy", 
   @"Your gullible", @"Say cheese", nil]; 
    }
    return _quotes;
}

//randomQuote twice
- (NSString *) randomQuote {

    return @"Some Quote";
}   

@end

and here is what you should have

#import "Quote.h"

@implementation Quote

//quotes method
- (NSArray *) quotes {
if (_quotes == nil){
   _quotes = [[NSArray alloc] initWithObjects:@"Happy, Happy, Happy", 
   @"Your gullible", @"Say cheese", nil]; 
    }
    return _quotes;
}

//random quote method
- (NSString *) randomQuote {

    return @"Some Quote";
}

@end