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

Rashii Henry
Rashii Henry
16,433 Points

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 feel as if my code is correct. but somehow i still need to alloc and initialize the "quotes" array property.

import "Quote.h"

@implementation Quote

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

@end

Rashii Henry
Rashii Henry
16,433 Points

hmm, thats pretty weird.

I've tried everything from removing the spaces to entering it on different lines.

Someone has to know the answer though .

13 Answers

This works all 3 tasks.

(1)@property (strong, nonatomic) NSArray *quotes;

(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"; }

@end

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

}

@end

Sakina Burch
Sakina Burch
1,853 Points

Thank you I was getting too frustrated. Small errors can kill.

The way taught is a bit verbose. I personally prefer allocating and initialising the array as @[ @"one", @"two, "three" ]. Much more tidy than alloc and initWithObjects. Also would it be better to check that there is not an array !_quotes rather than checking for nil?

#import "Quote.h"

@implementation Quote

-(NSArray *) quotes 
{ 
    if (!_quotes) _quotes = @[ @"1",@"2",@"3"]; 
        return _quotes;
}

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

@end
Michael Maloof
Michael Maloof
4,581 Points

Treehouse is awesome but omg these challenges are a nightmare. I'm starting to dread them and am contemplating just skipping them. Please just have an answer key. I shouldn't have to waste an hour digging through forums to find an answer. Not one of these answers has worked for me and literally I'm just copying and pasting it. The questions are normally poorly worded and aren't clear in what they are asking. After an X amount of tries TreeHouse should give me the answer and explain to me common mistakes other students are making on the problem (perhaps Im making the same mistake).

Some times it is easier to learn by seeing the answer and working backwards as opposed to spending hours solving a problem.

Amit Bijlani
Amit Bijlani
Treehouse Guest Teacher

I'm sorry that you are having problems but the code challenges are modeled after the code that has been demonstrated in the previous video. Most of the time re-watching the video should give you a good idea of what the challenge is asking you to do. Our job is to make sure that we drill in the concepts and part of it is making it a little challenging. We have a lot of debate regarding presenting the answer key and our fundamentally against it because we believe that does not help you learn.

If you have issues then I would encourage you to please open up a new thread and paste the code you have tried. As you can see this thread is very old and all over the place.

Michael Maloof
Michael Maloof
4,581 Points

Amit Bijlani thanks for the quick response/help!

I think this is a debate over learning methods more than this specific coding challenge. I respect Tree House's opinion (and the opinion of most learning institutions) that students learn through failure and solving the problem BUT it comes to a point where as a student, the time it takes to solve the problem is no longer worth the value in learning the solution. I don't necessarily think the answer should be given but the forums need to at least be easier to search through. Currently I'm having to use Google to find the proper Tree House forum. Old forums and answers that are posted but are incorrect need to be cleaned out. After it is clear that the student has tried multiple times (maybe 30 minutes-an hour on the problem) and looked through the forums, someone should reach out to him or her. Normally, when I reach this point it is because one of two things:

1) I'm not understanding the question (or it is a poor question) 2) I have a small error that I keep missing (i.e. a missed quotation mark)

Being stuck for one of those two reasons does not help me learn. It just causes frustration, lowers my want to learn the subject, and thus encourages me to spend less time learning to code. I think a couple solutions could solve this (again this is just an opinion I'm sure other students have different solutions in mind)

1) On multiple choice questions, if I need 4/5 to move on and I get 4/5, I should be showed the correct answer to the question I missed. I (and probably most students) am far too lazy to re-watch a 5-15 minute video again to find the answer to a question I don't need to move on in the program. If I don't get 4/5 correct, then don't show me the correct answer to discourage students from just clicking random answers to see the correct answer.

2) Forums need to be cleaner (1 for each question) and if someone posts a correct answer saying it is wrong that answer needs to be either removed or have a comment saying it is wrong. The forums need to be easier to search for also.

3) There should be a time limit on how long it takes a student to answer. They can take longer than the time given but after every X amount of time a new hint should be given. By a hint, I mean an actual hint with example lines of code.

4) The program should tell me which lines of code are correct and which aren't. This will at least tell me where my problem is so I don't spend an hour looking through my control.m when the problem is in my control.h

Thanks again for the quick response. I really do love Tree House and have been learning a TON on objective C. These are just one student's opinions. I'm sure other people aren't having an problems at all with the coding challenges.

I ran into a lot of trouble with this one and was getting frustrated because I was positive my code was correct...

I originally set up Task 1 using readonly but didn't declare it in @interface Quote : NSObject { }, and it accepted that. Then when I went to compile Task 2 it just wouldn't accept anything - even after I went back and fixed Task 1.

The solve was to restart the quiz fresh and run through it without using readonly.

Thanks, Robert. You were 100% correct. I tried this over and over with the "readonly". After I retyped my code without it I passed both sections. I was getting worried I wasn't understanding something from the video.

You saved me so much time.... I was getting a little bit frustrated being positive my code was correct... I did set up the quotes property as readonly and everything failed... After that I restarted it and I did it without readonly. Thanks!

Nic Huang
PLUS
Nic Huang
Courses Plus Student 10,573 Points

Try to create a "quotes" method and type your answer below instead of using randomQuote method. I got confused first, but I believe this is asking you to create a quotes method.

Rashii Henry
Rashii Henry
16,433 Points

Sorry for not mentioning the fact that i can only type my code after the first curly brace behind the randomQuote method.

so i believe creating a quotes method would be out of the question.

besides when i type anything else or have the slightest mistake in my code the compiler will tell me that my step one is incorrect.

this is the only code that i can type with no errors and still be incorrect.

Nic Huang
Nic Huang
Courses Plus Student 10,573 Points

I just retake the challenge and passed that again, using my above suggestion.

You need to create "quotes" method that returns an array instead of typing into "randomQuote" method.

Besides, your code lacks one important step, checking if _quotes is nil.

Then I think you will pass the challenge.

Amit Bijlani
Amit Bijlani
Treehouse Guest Teacher

The Code Challenge is following exactly what is explained in the preceding video: http://teamtreehouse.com/library/ios-development/build-a-simple-iphone-app-2/refactoring-into-a-model/understanding-property

You need to create a "getter" method for your _quotes instance variable so that when you reference self.quotes in the randomQuote method you are calling that "getter" method.

Rashii Henry
Rashii Henry
16,433 Points

okay Amit and Nic, if I'm doing this correctly, creating a getter method for _quotes would look like this:

- (NSArray *) quotes;        // (but no matter where i place this in the implementation i cant seem to get it right.)

with that created i can now refer to self.quotes . Now my code looks like this.

#import "Quote.h"

@implementation Quote

- (NSString *) randomQuote {    

if (self.quotes == nil){
        self.quotes = [[NSArray alloc] initWithObjects:@"1",@"2",@"3", nil];
}

    return @"Some Quote";
}

@end

however, my error is saying that i still need to alloc and initialize the quotesArray property.

Amit Bijlani
Amit Bijlani
Treehouse Guest Teacher

As the name implies you need to implement a getter method. Which means you cannot simply declare it by specifying this: - (NSArray *) quotes;. You need to fill out the guys of the method. What is it going to do? How is it going to allocate, initialize and return an array?

- (NSArray *) quotes  {
    if (_quotes == nil){
       _quotes = [[NSArray alloc] initWithObjects:@"1",@"2",@"3", nil];
    }
    return _quotes;
}
Rashii Henry
Rashii Henry
16,433 Points

Amit, how come when I try to go and implement the getter method in the header file just how the NSString is implemented in the header file it won't compile correctly?

Amit Bijlani
Amit Bijlani
Treehouse Guest Teacher

Can you paste your code? I'm not sure I follow.

Rashii Henry
Rashii Henry
16,433 Points

here is the header file for the code challenge. Part 2 of 3.

@interface Quote : NSObject { 
    NSArray *_quotes;
}

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

(NSString *) randomQuote;
@end

Now here is the implementation file.

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

i passed the challenge, but my question is how come i cant just place the -(NSArray *) quotes; getter method inside of the header file just like the -(NSString *) getter method is already inside of the header file. Is there a difference? or vice versa, why isn't there just an @property for the NSString like i created for the NSArray.

I believe my real question is, why aren't there just two getter methods or just two properties created? is there a reason why they have different approaches?

Tricia Martin
Tricia Martin
19,604 Points

I can't get this to pass either. I don't know what I'm missing. It keeps telling me Task 1 no longer passes.

Quote.h

@interface Quote : NSObject {
    NSArray *_quotes;

}

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

- (NSString *) randomQuote;

@end

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 @"Some Quote";
}

@end

Edit: Nevermind, the "readonly" was messing it up. Can I not delete posts?

Rashii Henry
Rashii Henry
16,433 Points

Hey Patricia,

i ran across that same problem and still am. but, i noticed when you take the readonly attribute away from the quotes property, it will then prompt you with the error that you need to allocate and initialize the array.

i have my implementation file the same as how i have it typed in xcode but i just cant seem to get pass the challenge. however, i have no runtime errors or any warnings in xcode.

But i keep trying though.

Rashii Henry
Rashii Henry
16,433 Points

Nevermind, i realized i was placing my code within the curly braces of the randomQuotes method.

Thanks for posting this, I too was including the readonly and thought I was going crazy.

Mark McCuller
Mark McCuller
3,539 Points

Rashii and Patricia,

This is what I did to pass this code challenge: Task 1 of 3 in the .h file. // Add property here @property (strong, nonatomic) NSArray *Quote;

Task 2 of 3 in the .m file.


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

  • (NSString *) randomQuote {


Task 3 of 3 in the .m file.

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

Rashii Henry
Rashii Henry
16,433 Points

thanks alot Mark,

but i did already pass the challenge. Although i do appreciate your input. it was still helpful.

it is wrong

it is wrong

Thanks that worked with the - sign in places.

Micah Elias
Micah Elias
2,774 Points

Hi! None of these answers worked for me. This is the code that I tried using

Rashii Henry
Rashii Henry
16,433 Points

show me what you're using and what is the error message.

The following code worked for me.

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

Same problem...Whats wrong with this code:

import "Quote.h"

@implementation Quote

  • (NSString *) randomQuote {

    return @"Some Quote"; }

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

} return _quotes; }

@end

Got it, syntax error....

Stefan Bjornsson
Stefan Bjornsson
3,362 Points

I´m revisiting this challenge after finishing "Build a simple Iphone app" and ran into a problem in task 2. 1st task is easy: @property(strong, nonatomic) NSArray *quotes; But in task 2 what concerns me I get an error "Remember to use the 'arc4random_uniform()' function and return a quote as a string object.". According to the answers above the arc4random_uniform() function is going to be used in task 3 and also none of the code provided above passes task 2 which is very strange. Is the challenge working?

Tried using literal notation instead of alloc/init and it didn't like that =(

Just had this pain as well, i removed readonly and i think some may not have squiggly bracket at bottom of original code near end.