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

Refactoring code challenge

Having more problems with the code challenge. Managed to get past the 1st and 2nd however the 3rd one is causing it to crash. I've added my code below, if anyone can point me in the right direction it would be much appreciated

int random = arc4random_uniform (self.quotes.count); return [self.quotes objectAtIndex:random];

4 Answers

I don't see any problem with this. What is the error you are getting on the right hand side?

Regards Ravi

Thats the thing there's no error on the right hand side its. At the top it says "Bummer! Try again" in red, I'm assuming its an error within the coding engine!?

Ok here it goes

Stage 1

@property (strong, nonatomic) NSArray *quotes;

Stage 2

import "Quote.h"

@implementation Quote

  • (NSArray *) quotes { if (_quotes == nil) { _quotes = [[NSArray alloc] initWithObjects:@"1",@"2",@"3", nil]; } return _quotes; }

  • (NSString *) randomQuote {

    return @"Some Quote"; }

Stage 3

int random = arc4random_uniform (self.quotes.count); return [self.quotes objectAtIndex:random];

As I say the first 2 stages seem to pass but the last one doesn't- appreciate any help Amit

Amit Bijlani
Amit Bijlani
Treehouse Guest Teacher

The code from stage 3 needs to on inside the randomQuote method because you are returning a random quote instead of @"Some Quote".

I'm having the same problem that Sam did, but I don't understand your response.

My code for stage 3:

#import "Quote.h"

@implementation Quote

- (NSArray *) quotes {
            if (_quotes == nil){
            _quotes = [[NSArray alloc] initWithObjects:@"0", @"1", @"2", nil];
      }
        return _quotes;
}

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

Hi I'm not have any success with the second stage of the challenge I don't understand what is wrong with my code. So here's my code any help will be greatly appreciated.

#import "Quote.h"

@implementation Quote

(NSArray *) quotes { 
_quotes = [[NSArray alloc] initWithObjects:@"11",@"22",@"33", nil];

return _quotes; 
}

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

@end

The error message I get after running the code says "Bummer! Remember to allocate and initialize the instance variable '_quotes' and return it from the method named 'quotes'."

Thanks,

Liam

Hi Liam, You are missing an if statement before you allocate your _quotes array.

Thanks that has helped me lots Amit has also pointed out an issue with my code, I was missing the '-' before my method.

Thanks for your help Liam

Cool yeah thanks for that, saw my mistake on that one, seems to have worked. Although I did still get pop up stating that there has been an error contact the help desk.

Amit Bijlani
Amit Bijlani
Treehouse Guest Teacher

You might see that error if you click on the "Check My Work" button too frequently. The engine does a lot of work behind the scenes once you click that button. If you click it too much then it might time out occasionally.