Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Objective-C Basics (Retired) Foundation Framework NSString

Paola Pimentel
Paola Pimentel
2,755 Points

My code is not working, can anyone tell me why? :(

In the code Challenge there asking me to: Declare a third NSString variable named 'favorite' and assign a concatenated string to it by appending the variable named 'color' to the variable named 'preference'. (Remember to use the method 'stringByAppendingString').

My last code goes like this: "NSString *favorite = [[color stringByAppendingString:preference] stringByAppendingString:@"favorite"];"

It keeps telling me that I named incorrectly 'favorite' but I don't understand why.... I'm stuck :(

2 Answers

Justin Horner
STAFF
Justin Horner
Treehouse Guest Teacher

Hello Paola,

Once we have the first two NSString variables we end up with the following.

NSString *color = @"Purple";
NSString *preference = @"My favorite color is ";

The next step in the challenge is asking you to append the color string to the end of the preference string. So in this case you need only one call to stringByAppendingString on the preference string. This will take the existing preference string and append the color string at the end.

NSString *result = [firstString stringByAppendingString:secondString];

I hope this helps.

Paola Pimentel
Paola Pimentel
2,755 Points

Thnx! I removed the last 'favorite' string but it didn't work, all I had to do is change the place of the other two too, I was so upset I didnt see it... thnx again!

Stone Preston
Stone Preston
42,016 Points

you only need to append one string, you are currently appending 2 strings. so remove the second call to the method:

NSString *favorite = [color stringByAppendingString:preference];

however that is still not quite correct. the value of that string is "Purple My favorite color is " which is incorrect.

the argument to the stringByAppending string method is appended on to the string you called the method on so

//the argument to the stringByAppendingStringMethod is appended to the end of the string you are calling the method on
NSString *someString = [@"this is " stringByAppendingString:@"a string"];

the value of someString is "this is a string"

since you have a string called preference and a string called color, you need to append the color string on to the end of preference. remember, the string you call the method on has the argument appended on to the end of it (hint: you need to call the method on the preference variable)