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 trialJames Husted
937 PointsRefactoring into a Model: Challenge task 2 of 3
I cannot see what is wrong with my code below when answering task 2... "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."
#import "Quote.h"
@implementation Quote
- (NSArray *) quotes {
if (_quotes == nil){
_quotes = [[NSArray alloc] initWithObjects:@"What ",
@"is ",
@"wrong?", nil];
}
return _quotes;
}
It keeps telling me "Bummer! Remember to allocate and initialize the instance variable '_quotes' and return it from the method named 'quotes'."
7 Answers
Amit Bijlani
Treehouse Guest TeacherYour answer should work unless you deleted the rest of the implementation that had the randomQuote
method and the @end
?
James Husted
937 PointsThe rest of the implementation is still there...
import "Quote.h"
@implementation Quote
-
(NSArray *) quotes { if (_quotes == nil){ _quotes = [[NSArray alloc] initWithObjects:@"What ", @"is ", @"wrong?", nil];
} return _quotes; }
-
(NSString *) randomQuote {
return @"Some Quote"; }
@end
Amit Bijlani
Treehouse Guest TeacherI just pasted that answer and it passed.
James Husted
937 Pointshmm, not sure why it hates me...
James Husted
937 PointsSorry, I just learned about Markdown, below should be a better view of the code snippet
``` Objective C
import "Quote.h"
@implementation Quote
-
(NSArray *) quotes { if (_quotes == nil){ _quotes = [[NSArray alloc] initWithObjects:@"What ", @"is ", @"wrong?", nil];
} return _quotes; }
-
(NSString *) randomQuote {
return @"Some Quote"; }
@end
hopefully that is more readable. :)
Paul Yorde
10,497 PointsHi James,
Are you certain you are writing the code to the correct file? It should be 'Quote.m.'. I pasted your code from above, and it works.
N B
2,040 PointsThe code is correct, for Quote.m Make sure the array property in challenge 1 (Quote.h) isn't set to readonly.
Michael McLaughlin
14,033 PointsI'm having this problem too. I tried both my answer, (same syntax and everything) in both chrome and firefox. what gives?
Michael McLaughlin
14,033 Pointsok, I think I figured it out, you can't forget to set the output data type to NSArray both on the .h and implementation file.