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 trialJoseph Rimar
17,101 PointsCustom Class Code Challenge
I'm trying to implement a method called 'quotes' which will only allocate and initialize if _quotes is 'nil'. I seem to have something wrong but I cannot figure it out. Please help!
Here is my code:
-(NSArray *) quotes {
if (_quotes == nil) {
_quotes = [[NSArray alloc] initWithObjects:
@"It's Sunny Today",
@"It's cold",
@"It's hot", nil];
}
return _quotes;
}
2 Answers
Gareth Borcherds
9,372 PointsThere is nothing wrong with your code. Do you put it in the wrong spot? I copied and pasted your code into the .m file. Here is what the full code looks like. See if that helps.
#import "Quote.h"
@implementation Quote
-(NSArray *) quotes {
if (_quotes == nil) {
_quotes = [[NSArray alloc] initWithObjects: @"It's Sunny Today", @"It's cold", @"It's hot", nil];
}
return _quotes;
}
- (NSString *) randomQuote {
return @"Some Quote";
}
@end
Joseph Rimar
17,101 PointsHey Gareth, I guess it was an error in the treehouse compiler. I refreshed the page and attempted it again with EXACTLY the same code and it worked. Not going to lie it was driving me crazy though. Thank you for the help!