typeoneerror typeoneerror - 6 months ago 27
Objective-C Question

Shortcuts in Objective-C to concatenate NSStrings

Are there any shortcuts to (

stringByAppendingString:
) string concatenation in Objective-C, or shortcuts for working with
NSString
in general?

For example, I'd like to make

NSString *myString = @"This";
NSString *test = [myString stringByAppendingString:@" is just a test"];


something more like

string myString = "This";
string test = myString + " is just a test";

Answer

Two answers I can think of... neither is particularly as pleasant as just having a concatenation operator.

First, use an NSMutableString, which has an appendString method, removing some of the need for extra temp strings.

Second, use an NSArray to concatenate via the componentsJoinedByString method.

Comments