How to concatenate a string in Objective-C

I’m writing this article with the objective of hopefully helping you if you’re looking how to concatenate a string but also to vent just a tiny little bit.

Firstly, to set the scene. You have two strings and you want to join them together. For example, string1 is something like “Fly” and string2 is something else like “Digital” and you want to join them as string3 to become something new such as “Fly Digital”.

In ASP Classic, this is a breeze:

string3=string1 & " " & string2

In PHP, not a problem:

$string3 = $string1 . " " . $string2;
echo $string3;

But Objective-C doesn’t quite use the same logic of string1 + string2 = string3. Instead you need to call a whole new method called stringByAppendingString, which is where I start to ask questions. But first, here’s how I do it in Objective-C:

NSString *string1 = @"Fly";
NSString *string2 = @"Digital";
NSString *string3;
string3 = [string1 stringByAppendingString:string2];

Now call me old fashioned, but I think the PHP / ASP Classic / Javascript methodology is far more easily adoptable for programmers than having to know a new method to achieve what I consider a very basic but fundamental part of programming. I question why the developers of Objective-C thought that a whole new method had to be invented rather than sticking to the very logical 1 + 2 = 3 style. If there’s anyone reading this that knows the answer to that question I’d love to hear it.