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 Objective-C Basics (Retired) Foundation Framework NSString

Bachir Kurdi
Bachir Kurdi
10,098 Points

what is wrong ?

The challenge is : 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 code is: NSString *color = @"purple"; NSString *preference = @"My favorite color is"; NSString *favorite = [color stringByAppendingString : preference];

what have i done wrong ?

5 Answers

Wenting Shi
Wenting Shi
13,562 Points

Hi, you should call the stringByAppendingString method on variable preference instead of color.

Like this: NSString *favorite = [preference stringByAppendingString:color];

:)

Hi Bachir,

You almost have it! switch the preference and color around.

NSString *color = @"Purple";
NSString *preference = @"My Favorite Color is";
NSString *favorite =  [preference stringByAppendingString:color];

output 2014-07-26 23:02:10.007 Test [4821:607] My Favorite Color is Purple

Bachir Kurdi
Bachir Kurdi
10,098 Points

ok THANKS it worked but can you explain why it had to be like that and not the other way ?

Wenting Shi
Wenting Shi
13,562 Points

Hi, for example:

NSString *string = [@"Hello," stringByAppendingString:@" World!"];

will give you "Hello, World!", this is just the way the method is implemented.

whenever you feel confused about how to use a function, just use shortcut "option + click" to access the apple documentation.

Have a good one!

Bachir Kurdi
Bachir Kurdi
10,098 Points

ok thanks guys you have been a great help :)