-
-
Save xeoncross/a5d703f3fae68ab3fd5e to your computer and use it in GitHub Desktop.
// JS | |
var one = "Hello", | |
var two = "World"; | |
var three = one + " " + two; | |
// Go | |
one, two := "Hello", "World" | |
three := one + " " + two | |
// PHP | |
$one = "Hello"; | |
$two = "World"; | |
$three = $one . " " . $two; | |
// Swift | |
let one = "Hello" | |
let two = "World" | |
let three = one + " " + two; | |
// Objective-C | |
NSString *one = @"Hello"; | |
NSString *two = @"World"; | |
NSString *three = [[one stringByAppendingString:" "] tringByAppendingString:two] |
Even if just concatenating
[one stringByAppendingString:two]
Thanks @tomsterritt, that did help shorten the code.
@invariant, my mistake using NSLog/Println in the examples. The point was to concatenate strings, not find a quick way to print them.
All of these issues are because of backwards compatibility with C, though.
You have to have @'s in front of all the literal a because, in C, "" is a char*, {} denotes scope, structs, or arrays, etc.
And they can't override + to concatenate strings, because that breaks C compatibility. +, with two pointers, adds their addresses.
You concatenation with a space can be done with:
NSString *three = [NSString stringWithFormat:@"%@ %@", one, two];
This is because of Objective-C's Smalltalk routes. It's verbose because the syntax was designed for message passing. Text processing is very verbose in Objective-C. Apple did a great job of modernising the language to 2.0 which cuts down a little on the boilerplate. Swift takes things further.
This will work:
NSString *three = @"Hello" @" " @"World";
All but the first @
are optional.
Poor example. There's no need for stringWithFormat here. Just do:
NSLog("%@ %@", one, two);
Also you can write:
NSString *one = @"Hello", *two = @"World";