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

Code Challenge: Creating a Custom Class

Need some help with Code Challenge, thank you

Challenge: ~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.

Code:

import "Quote.h"

@implementation Quote

  • (NSString *) randomQuote {

    return @"Some Quote"; }

  • (NSArray *) quotes { if (_quotes == nil){ _quotes = [[NSArray alloc] initWithObjects:@"It is certain", @"You bet ya", @"Without any doubt", nil];}

    return _quotes;
    

    }

@end

The error message isn't helping and the code "AS IT ALWAYS DOES" compiles in Xcode just fine.

Again, thank you for any help here.

6 Answers

Stone Preston
Stone Preston
42,016 Points

i tried your code and it passed the task fine. below is my .m:

#import "Quote.h"

@implementation Quote

- (NSArray *) quotes { 
  if (_quotes == nil){ 
  _quotes = [[NSArray alloc] initWithObjects:@"It is certain", @"You bet ya", @"Without any doubt",
  nil];
  }

return _quotes;
}

- (NSString *) randomQuote {

    return @"Some Quote";
}

@end

and here is the .h:

@interface Quote : NSObject

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

@end
Patrick Donahue
Patrick Donahue
9,523 Points

Should be self.quotes = [NSArray alloc] init......

Stone Preston
Stone Preston
42,016 Points

actually his code uses the correct method, initWithObjects since the task asks to allocate and initialize the array with three strings.

Stone Preston
Stone Preston
42,016 Points

and also shouldnt be self.quotes since we want to access the _quotes ivar directly in this getter method for the quotes property.

OK, thank you guys for all the help. Adding ".self" didn't work and I have to say (I am learning here so this is just my thought process) adding self to an alloc and init method doesn't makes sense. Again I am trying to understand the code. Thank you again and I will post any updates.

I fixed the problem, in my header file I had readonly in my property declaration. While it didn't cause a problem in the first example the second example didn't like it.

Can anyone elaborate just a little and tell me why that is? Thank you both for your help.

If you are going to have readonly in your property declaration, you must create your own instance variable in the interface file. For example: '''Objective-C @interface Quote : NSObject { NSArray *_predictions; }'''

If you are going to have readonly in your property declaration, you must create your own instance variable in the interface file. For example: @interface Quote : NSObject { NSArray *_quotes; }

If you are going to have readonly in your property declaration, you must create your own instance variable in the interface file. For example: @interface Quote : NSObject { NSArray *_quotes; }