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 trialPaola Pimentel
2,755 PointsMy 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
Treehouse Guest TeacherHello 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.
Stone Preston
42,016 Pointsyou 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)
Paola Pimentel
2,755 PointsPaola Pimentel
2,755 PointsThnx! 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!
Justin Horner
Treehouse Guest TeacherJustin Horner
Treehouse Guest TeacherYou're welcome!