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

Joe Silvasi
Joe Silvasi
3,133 Points

problem with code challenge 2 of 3; Build a Simple iPhone App(iOS7) - Refactoring into a Model

In code Challenge 1 I added

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

into Quotes.h When I enter

- (NSArray *) quotes {
  if (_quotes == nil) {
     _quotes = [[NSArray alloc] initWithObjects:@Quote1", @“Quote2", @Quote3", nil];
     }
   return _quotes;
}

into Quotes.m which seems to follow the sample code pattern presented in the video, I get an error: it tells me to allocate _quotes and return it. Looks like i've done that, so what is it complaining about? I don't see the issue and can't move forward to complete the stage.

4 Answers

Hello Joe,

First off in you .h where you declare _quotes don't call it that. call it just quotes because the underscore is used when calling it, it basically replace self. so it should look like this

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

try that, Kai

Joe Silvasi
Joe Silvasi
3,133 Points

Well, you are correct. However, after reviewing the video I note that in the design pattern I was also supposed to add an instance variable _quotes to the @interface in the .h file in addition to the property for quotes like this:

@interface Quote : NSObject { NSArray *_quotes; }

However that doesn't satisfy the "compiler" either and I still get the "Bummer! Remember to allocate and initialize the instance variable '_quotes' and return it from the method named 'quotes'." message.

Is this a code challenge, if it is I'll go do it and let you know where you went wrong.

Joe Silvasi
Joe Silvasi
3,133 Points

yes it is. Went back and reviewed the video a couple more times. (This specific design pattern is shown at 7:20 in the video.) I've also sent a group of screen shots to the support people but don't expect an answer until early in the week from them.

Joe Silvasi
Joe Silvasi
3,133 Points

No reply yet from tech support. I downloaded the Crystal Ball app at this stage to Xcode, then copied and pasted it's solution into the task 2 .h and .m files changing "prediction" to "quote" and reducing the number of array instances to just 3 + nil in the .m. Still no joy: it now tells me that "It looks like Task 1 is no longer passing."

I'm starting to get very annoyed at this and am considering canceling my membership If I can't get this to work and move on.

Hey Joe,

I wrote it and here is the answer,

1) .h

@property(strong, nonatomic)NSArray *quotes;

2) .m

-(NSArray*)quotes{
  if(_quotes == nil){
    _quotes = [[NSArray alloc]initWithObjects:@"hello", @"team", @"treehouse", nil];
  }

  return _quotes;
}

3) .m

- (NSString *) randomQuote {
    int random = arc4random_uniform(self.quotes.count);
    return [self.quotes objectAtIndex:random];
}

there we go ket me know if this works for you.

Happy hacking, Kai