views:

1957

answers:

5

Currently I am working on a piece of software, which is currently written 100% in Objective-C using the iPhone 3.0 SDK.

I have come to a cross roads where I need to do quite a bit or string concatenation, more specifically, NSString concatenation and so far I have been doing it like this:

Objective-C string concatenation:

NSString *resultantString = (NSMutableString*)[@"Hello " stringByAppendingString: @"World"];

Now as you can imagine this gets quite difficult to read when I try to concatenate 6 NSStrings together.

At first I contemplated mixing in an Objective-C++ class to do my string concatenation and hand it back to my Objective-C class as then I could use C++'s easy string concatenation like:

C++ string concatenation:

string cppString = "Hello" + "World" + "see" + "easy!";

I could use C char arrays but that would be a little more difficult to read.

It then struck me that I could use a Python or Ruby bridge in Cocoa (which provide the added bonus of Regular expressions and superior string handling than the C based languages do).

This made sense to me even though I have coded only small amounts of Ruby and Python, because they are billed as string manipulation languages. Perl I decided to skip because it isn't directly supported in Xcode.

I am also interested in performance, so I am looking for the fastest way to do my string concatenation.

So what should I do? Is there some deviously easy way I am missing in Objective-C to concatenate many strings at once say 10 strings? or is my idea to use Python or Ruby class methods that return a concatenated or regex modified strings not as incredibly insane as it sounds? Perhaps I even missed some another way to do this?

Update: Yes. It seems I was rather crazy for thinking of pulling in another language runtime to do string manipulation, especially since I noted that I was concerned with speed. Adding a bridge would probably be much slower than simply using NSString/NSMutableString.

+4  A: 

Have you seen the appendString method from the NSMutableString class?

appendFormat from the same class will let you do many concatenations with one statement if that is what you're really interested in.

Jonathan Arbogast
That looks good Jonathan. I am relatively new to Cocoa so I missed this method. Still interested as to what others think about mixing other languages for regex and general string manipulation. ta
Brock Woolf
Using an NSMutableString if you're concatenating a lot isn't a bad idea, but appendFormat: does not return anything, so that code example does not actually work.
Chuck
Building that in Xcode gives me a warning that NSString may not respond to -appendFormat. I noticed you are also missing the colon after 'appendFormat'.
Brock Woolf
Agreed, I removed my crappy off the cuff code that I typed directly into SO in haste. Overall, this is still the approach that I would take.
Jonathan Arbogast
+8  A: 

For fixed size concatenation, you can use [NSString stringWithFormat:] like:

NSString *str = [NSString stringWithFormat:@"%@ %@ %@",
                                  @"Hello", @"World", @"Yay!"];
notnoop
Not sure why Jonathan got the most votes for his answer, I couldn't get it to compile and it's missing a colon. This is simple and readable I think i will stick to this.
Brock Woolf
This seems like a less accurate response, given that you originally asked for code that returns "HelloWorldseeeasy!" sans spaces which Jonathan's does... :-)
Kendall Helmstetter Gelner
Programmers, we can't help but find technicalities just to be right :P
Brock Woolf
How many arguments does this take up to?
Brock Woolf
I don't know, but some really large number (e.g. max int)
notnoop
+7  A: 

you can use join operation.

NSArray *chunks  = ... get an array, say by splitting it;
string = [chunks componentsJoinedByString: @" :-) "];

would produce something like oop :-) ack :-) bork :-) greeble :-) ponies

Kthevar
+1 because I learnt something
teabot
yes nice idea. is the extra code you have to write worth it though i wonder.
Brock Woolf
+2  A: 

I would avoid mixing languages, particularly if you don't know Python or Ruby well. What you gain in readable code in your Objective-C you will loose have having to read multiple languages to understand your own code base. Seems like a maintainability nightmare to me.

I'd strongly suggest taking one of the suggestions of how to do this in Objective-C directly.

acrosman
That is a good point and one that I didn't happen to think about when I had this idea at 2 in the morning :) +1 for restoring my sanity :D
Brock Woolf
+2  A: 

Dragging in C++ just for this seems quite heavy-handed. Using stringWithFormat: on NSString or appendFormat: with an NSMutableString, as others have suggested, is much more natural and not particularly hard to read. Also, in order to use the strings with Cocoa, you'll have to add extra code to convert back and forth from C++ strings.

Chuck