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

Vidit Shah
Vidit Shah
6,037 Points

NSString Task

NSString *color=@"Purple"; NSString *preference=@"My favorite color is"; NSString *favorite=[color stringByAppendingString:preference]; NSLog(@" %@",favorite);

//Its perfectly working in Xcode.But not in The environment provided by Treehouse.

I tried your code in Xcode

  NSString * color = @"Purple";

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

  NSString * favorite = [color stringByAppendingString:preference];

  NSLog(@" %@",favorite);

The result is

 PurpleMy favorite color is

If you change favorite to this

  NSString * favorite = [preference stringByAppendingString:color];

You got

My favorite color isPurple

Add a space to preference

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

5 Answers

NSString * favorite = [NSString stringWithFormat: "%@ %@", preference, color];

Vidit Shah
Vidit Shah
6,037 Points

Task is asking me to use method :StringByAppending String

use NSMutableString instead of NSString

NSMutableString * color = @"Purple";

Vidit Shah
Vidit Shah
6,037 Points

But thats not yet taught..!

Vidit Shah
Vidit Shah
6,037 Points

BRO everything is working fine is Xcode but in the compiler provided by them it states, Bummer! Make sure to assign the correct value to 'favorite'.

I think the correct answer should be

My favorite color is Purple

But in your code shows

 PurpleMy favorite color is

as the answer

Vidit Shah
Vidit Shah
6,037 Points

Ya that was correct :P.

You are welcome, Please mark my answer as correct, so I can get the points :D