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

Code Challenge : Creating a Custom Class

I am at the step 2 of the code code challenge and having a difficult time trying to understand what i am doing worn here,#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";
}

@end

13 Answers

Andrew Elliott
Andrew Elliott
6,003 Points

Basically to complete stage two, more code than described is needed (what constitutes stage three has been included in the validation for stage two).

My stage two code (which validates)

Quote.h

@interface Quote : NSObject

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

@end

Quote.m

#import "Quote.h"

@implementation Quote

- (NSArray *) quotes {
    if (_quotes == nil) {
    _quotes = [[NSArray alloc] initWithObjects:@"one", @"two", @"three", nil];

  }
 return _quotes;
}

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

@end

Then on the next screen (stage 3), just press "check work" (without adding anything).

Sam Bass
Sam Bass
9,038 Points

Thank You! This code does work.

Amit Bijlani
STAFF
Amit Bijlani
Treehouse Guest Teacher

Are you getting any errors? Your code looks good as per Task 2. You can click the 'Preview' button to see any compiler errors.

Yes I am getting an error. - 'Oops! It looks like Task 1 is no longer passing.'

@interface Quote : NSObject {
NSArray *_quotes ;

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

- (NSString *) randomQuote;

@end
Amit Bijlani
Amit Bijlani
Treehouse Guest Teacher

You need not create an instance variable _quotes and remove the readonly attribute from the quotes property. Those were not part of the instructions which is why your challenge is not passing.

Antonio Fullone
Antonio Fullone
1,019 Points

Sorry to enter in the discussions, but I am also stack with this task. It seems that for my browser I'm always missing some questions, I have this situation (on safari with Mac Mavericks) Step 2 of 3.

Screenshot

The question says : "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."

I try to do it and when I submit it says : "Bummer! Remember to use the 'arc4random_uniform()' function and return a quote as a string object." But I cannot understand where the functions comes from, is not asked in the question. Also few days ago I had the same problem and I found out I was missing a string not mentioned in the question, is it possible that with safari I miss something in the questions? Hope the image is visible.

Thanks in advance.

Amit Bijlani
Amit Bijlani
Treehouse Guest Teacher

The message definitely does not match up to what is wrong. You are missing a return statement in your quotes method. Firstly, the method should be named "quotes" and not "_quotes" and you need a return statement: "return _quotes;" as the last statement of that method.

same here. i get the "Bummer! Remember to use the 'arc4random_uniform()' function and return a quote as a string object." Message. My Code my Quote.m:

#import "Quote.h"

@implementation Quote

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

    return _quotes;
}

@end

my Quote.h:

@interface Quote : NSObject

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

@end
Amit Bijlani
Amit Bijlani
Treehouse Guest Teacher

Just like in the video you need to modify the randomQuote by generating a random number using the arc4random_uniform() function and pass it back a rand quote from the quotes array.

Andrew Elliott
Andrew Elliott
6,003 Points

I had the same problem - the arc4random_uniform() warning appeared in stage 2 (although it isn't mentioned in the instructions for that stage), then on completing stage 2 (with the arc4random_uniform in place) stage 3 asked me to "Within the implementation file we need to modify the 'randomQuote' method to return a random quote from the 'quotes' array using the 'arc4random_uniform()' function" (which had to be in place to get through stage 2).

Simply pressing check work at this stage 3 passes the challenge, as the code for stage 3 was required to pass stage 2 - possible bug in the challenge?

cant step to stage 3. Got the same message....i will skip this. its really frustrating...

I'm having trouble with step 3 of the task.

My Quote.m:

#import "Quote.h"
@implementation Quote
- (NSArray *) quotes {
    if (_quotes == nil){
        _quotes = [[NSArray alloc] initWithObjects:@"1", @"2", @"3" ,nil];
    }
    return _quotes;
}
- (NSString *) randomQuote {
    int random = arc4random_uniform();
    return [self.quotes objectAtIndex:random];
}
@end

My Quote.h:

@interface Quote : NSObject
// Add property here
- (NSString *) randomQuote;
@property (strong, nonatomic) NSArray *quotes; 
@end

EDIT: Issue resolved.

Aaron S
PLUS
Aaron S
Courses Plus Student 2,072 Points

I had difficulty getting code to pass but this is what finally passed for me :


Quote.h


<p>@interface Quote : NSObject

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

  • (NSString *) randomQuote;

@end</p>


Quote.m


<p>#import "Quote.h"

@implementation Quote

-(NSArray *) quotes { if (_quotes == nil) { _quotes = [[NSArray alloc] initWithObjects:@"one", @"two", @"three", nil];

} return _quotes; }

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

}

@end</p>

Yea... this challenge is super flukey and is really frustrating me too. I have the code in Xcode build JUST FINE. However I come to this challenge, put the same thing in, and it keeps giving me an error, with or without putting the arc4random_uniform(self.predictions.count); return [self.predictions objectAtIndex: random];

Whatever, I don't think the points really matter, as long as I'm learning the material, however it WOULD be nice if it wasn't so frustrating.... That little wheel with the +6 rainbow at the end is what really makes me keep using this site HAHA.

okay, i just caught my mistake... I was saying predictions the whole time... but even correcting for that, I'm still getting an error and even copy and pasted what they have above and its still not working....

EDIT: I guess the update did go through... I thought it wanted me to do all of it at once... Sorry for clogging up the discussion boards, I finally made it.

BTDUBS, that video you guys made was totally worth it. I love the "cinematic" effects, the macro close up shot of that flower with the ants on it, and the tiny story line you guys got going on. It does intrigue me indeed (:

Amit Bijlani
STAFF
Amit Bijlani
Treehouse Guest Teacher

Sorry for any confusion the hint has been updated to reflect what is required from task 2.