Note that there are some explanatory texts on larger screens.

plurals
  1. POObjective-C string manipulation
    text
    copied!<p>Currently I am working on a piece of software, which is currently written 100% in Objective-C using the iPhone 3.0 SDK.</p> <p>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:</p> <p><strong>Objective-C string concatenation:</strong></p> <pre><code>NSString *resultantString = (NSMutableString*)[@"Hello " stringByAppendingString: @"World"]; </code></pre> <p>Now as you can imagine this gets quite difficult to read when I try to concatenate 6 NSStrings together.</p> <p>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:</p> <p><strong>C++ string concatenation:</strong></p> <pre><code>string cppString = "Hello" + "World" + "see" + "easy!"; </code></pre> <p>I could use <strong>C</strong> char arrays but that would be a little more difficult to read.</p> <p>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).</p> <p>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.</p> <p>I am also interested in performance, so I am looking for the fastest way to do my string concatenation.</p> <p>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?</p> <p><strong>Update:</strong> 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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload