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



Just wondering about the stringByAppendingString method. The code from the lesson is as follows:

    NSString *alfa = @"alfa";
    NSString *delta = @"delta";
    NSLog(@"%@ %@", alfa, delta);

    NSString *concat = [[alfa stringByAppendingString:delta] stringByAppendingString:@"lotsmore"];

    NSLog(@"%@", concat);

How come you don't need to declare *lotsmore and initialise it?


P.S. By the by, writing 'My favorite color is purple' is like nails against a blackboard for an Australian!

1 Answer

Heidi Brebels
Heidi Brebels
21,620 Points

You're not using "lotsmore" here as a variable, but as a value. Probably because it's not needed as a variable. Why would you declare something a variable if you don't need it more than once or if the value is likely to change often?

It's probably also not a variable here just to show you that you can use a variable and an actual value interchangeable.

You can declare it a variable:

NSString *lotsmore = @"lotsmore";

NSString *concat = [[alfa stringByAppendingString:delta] stringByAppendingString:lotsmore];

But at the same time, you can declare nothing a variable:

NSString *concat = [[@"alfa" stringByAppendingString:@"delta"] stringByAppendingString:@"lotsmore"];