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 trialbronnyreinhardt
1,007 PointsstringByAppendingString
Hey,
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?
Cheers
P.S. By the by, writing 'My favorite color is purple' is like nails against a blackboard for an Australian!
1 Answer
Heidi Brebels
23,239 PointsYou'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"];