After years of hard work you finally feel confident with the verbose-long-tedious Objective-C syntax. Well done! But… something has changed and it’s time to study! Again.
Fear not guys, I’m just joking, indeed what I’m going to show you today will improve your coding style. Say welcome to Literals, a new feature that I’m sure you are going to appreciate!
Literals have been recently introduced into the LLVM compiler. They essentially add a shortcut to create some specific data types that allows you to work with them in an easier way.
It’s one of those times in which code “speaks” better than words, so let’s see some examples for each category.
NSNumber
Up until now, when we had to instantiate a NSNumber, it was something like this:
NSNumber *integer = [NSNumber numberWithInt:19];
With Literals we obtain the same result writing:
NSNumber *integer = @19
As you may have noticed Literals use the “@” sign in the same way we use it to create NSStrings (just without double quotes) so it should be easy for you to remember how to do it 😀
Furthermore, C’s type suffixes may be used to define the size of numeric data:
NSNumber *unsignedInteger = @19U; //Unsigned Integer
NSNumber *longInteger = @19L; //Long Integer
NSNumber *floatNumber = @19.5493F; //Float
NSNumber *boolean = @YES; // //BOOL
NSArray
Creating and working with arrays in a language such as PHP is extremely simple. Objective-c syntax is quite different. It has a huge list of functionalities and I love the way you can work with collections in Objective-C but, it’s nowhere near as quick to write.
Literals come in handy to fill this gap. Let me show you some really useful short-hands which are going to simplify your code (and hopefully, your developer life):
This is a simple definition we are used to:
NSArray *array = [NSArray arrayWithObjects: [NSNumber numberWithInt:10],
@"A String!",
[NSNumber numberWithFloat:10.654F],
nil];
With literals we can make it way easier and more readable:
NSArray *array_l = @[@10, @"A string", @10.645F];
As you can see we can substitute the entire array definition with @[…] and we can also get rid of the final nil necessary to close the object list, because the compiler will substitute this literal with [NSArray arrayWithObjects:count:].
Another awesome improvement is related to the syntax to access the array elements. In the “classic” way we can use functions such as objectAtIndex:
id obj = [array objectAtIndex:0];
With literals, we can use square brackets syntax like we do in PHP, resulting again in a cleaner code:
id obj = array[0];
And we can obviously use this syntax to modify values in a mutable array:
NSMutableArray *mutableArray = [NSMutableArray arrayWithObject:@[@11,@76]];
mutableArray[0] = @51;
NSDictionary
NSDictionary require a quite long syntax to initialize a complete object. I don’t want to complain about that because I think that the verbosity of Objective-C helps to maintain focus on your code, but I have to admit that NSDictionary can be a bit too demanding 😛
Let’s see this definition:
NSDictionary *dict = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects: [NSNumber numberWithInt:10],
[NSNumber numberWithInt:20],
[NSNumber numberWithInt:30],
nil]
forKeys:[NSArray arrayWithObjects: @"first",
@"second",
@"third",
nil]];
I probably pushed this a bit too far, but it’s for the sake of clarity. In fact, we get useful benefits from literals in this situation too.
NSDictionary *dicts = @{@"first":@10, @"second":@20, @"third":@30};
Besides translating every NSNumber and NSArray definition, with literals we can use a really readable format which puts the key and its relative value side by side. This is by far my favorite new addition to Objective C.
As you probably understood by now, literals are something you’ll get to love as a developer, because they make your code much more readable and, as a direct consequence, way less error-prone! And that’s great!
Yari D'areglia
https://www.thinkandbuild.itSenior iOS developer @ Neato Robotics by day, game developer and wannabe artist @ Black Robot Games by night.