Last active
August 29, 2015 14:05
-
-
Save xeoncross/a5d703f3fae68ab3fd5e to your computer and use it in GitHub Desktop.
Objective-c is basically typing for the fun of it.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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] |
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.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.