NSString Concatenation (Category)

Hi folks. Just got back from a trip but I thought I would write you and tell you about this thing.

Sometimes, you just want to concatenate a bunch of strings together. There are several ways to do this. You can use stringByAppendingFormat with NSStrings or you can use append with NSMutableStrings. You can also add a bunch of strings to an array and generate a new string based on the array. Some languages overload the “+” operator to allow strings to be added together. Since Objective-C is a strict superset of C we can’t overload that operator.

So, what I’ve done is simply create a category on the NSString class. It contains a single class method called “concatenateStrings” that takes any number of arguments. Any number of NSString or NSMutableString arguments are concatenated together and returned as an NSString.

Get the example project from here.

Advertisements

One Response to NSString Concatenation (Category)

  1. […] while back I had posted on concatenating NSString objects. Now that we have literal notation, let’s see how much easier and terse we can make this, […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: