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 trialGanesh Gunaratnam
1,007 PointsCode 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
6,003 PointsBasically 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).
Amit Bijlani
Treehouse Guest TeacherAre you getting any errors? Your code looks good as per Task 2. You can click the 'Preview' button to see any compiler errors.
Ganesh Gunaratnam
1,007 PointsYes 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
Treehouse Guest TeacherYou 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
1,019 PointsSorry 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.
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
Treehouse Guest TeacherThe 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.
Antonio Fullone
1,019 Pointsok I'll try, thanks!
Jan Diekmann
1,067 Pointssame 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
Treehouse Guest TeacherJust 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
6,003 PointsI 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?
Jan Diekmann
1,067 Pointscant step to stage 3. Got the same message....i will skip this. its really frustrating...
Ronit Kumar
965 PointsI'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
Courses Plus Student 2,072 PointsI 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>
Justyn Huang
1,637 PointsYea... 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.
Justyn Huang
1,637 Pointsokay, 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
Treehouse Guest TeacherSorry for any confusion the hint has been updated to reflect what is required from task 2.
Sam Bass
9,038 PointsSam Bass
9,038 PointsThank You! This code does work.