Welcome to the Treehouse Community
Looking to learn something new?
Paola Pimentel2,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 :(
Justin HornerTreehouse Guest Teacher
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 Preston42,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)