Join Simon Allardice for an in-depth discussion in this video Using Objective-C literals, part of iOS 6 App Development New Features.
Objective-C Literals syntax is great and something that you are likely to use in everyday code. But let me be clear, what I am about to show you doesn't give you any new capability you didn't already have, but it does make it faster and easier to write code to create common objects. And you have already used this idea with NSString, if you've ever written a line of code likes this where you didn't say alloc and init or any variant of that, but you still end up with a NSString object because the at sign here is telling the compiler to go ahead and create that NSString object with these contents, it's a shortcut format to make it easier for you to create NSStrings.
Now this syntax has been expanded to include NSArray, NSDictionary, and NSNumber objects. And as with NSStrings, it's all about the at sign. That is the indicator to the compiler that you are about to begin using the literal syntax. Let's see some before and afters. So I've just got some simple code here inside a basic Command-line tool, and let's take the good old NSArray on line 22 here. This is the format used by many years of Objective-C developers, creating Arrays with the method alloc and init with objects or perhaps using array with objects, and you are providing a list of those objects, although taking care to terminate that list of objects with nil.
This is how you have to tell the NSArray that it's finished, that you've provided all the objects for it. And the objects I am passing here are just the ones I am creating on line 17 through 19, just basic NSString and NSNumber. Well, this is now the alternate way of creating an NSArray. I first define the pointer to it, NSArray *myArray= then I use the at sign, and I need to open up the square bracket here. And it gives me the little pop-up code sends that I just need to provide a list of objects.
So I will, I have a few that I am creating on those earlier lines, and then I am done, I just close the square bracket and finish the line. I no longer need the alloc, I no longer need init with object, and I no longer need the nil at the end. Now once again, this is not a new kind of NSArray, this doesn't provide anything different, it's simply more concise, this is easier to write and easier to read. Now just as there is that syntax to create them, there is also a shortened syntax to access the elements inside an NSArray once you've actually got one.
Now this is the conventional format that you're probably used to already, using the objectAtIndex method of NSArray. Well, this is the new format, name of the array and then as in many other programming languages, we can just use the square brackets here and give it the number of the element that we want. So if I want the second element, that's going to be number 1, zero based of course, again just simply a concise way to do this. Now one of the objects I am adding to this array here--this baz object--is this one that I am creating on line 19, and it's an NSNumber.
Because if you just want to put say a basic primitive value like an integer or a float inside a collection, you can't. You need to box it up, you need to make it a proper Objective-C object, and that's what NSNumber is for, it's a wrapper for a primitive value. So we commonly use methods like this, creating an NSNumber with numberWithInt. Although there is lots of these methods numberWithChar, numberWithFloat, numberWithLong. Well, NSNumber gets a literal syntax too, so I want to create a new NSNumber with the integer 99.
All I need to do is use the at sign and the number, and it's smart enough to understand whether we are providing an Int or a Float or a Double and so on. So for an integer, this is just like using a number with Int. If I passed in 99.345, that would be like using number with double, I'd add the F if I want it to make it a float explicitly. Now sometimes NSNumber is used to wrap a Bool primitive so you can also use Bool values, just an uppercase YES or uppercase NO.
Don't need any square brackets, don't need any double quotes, we just use the at sign and the value, it's as simple as that. And finally, we have NSDictionary, a set of key value pairs. So we are not accessing the elements by number, we are accessing them by key. And there is a few different ways to create an NSDictionary, but very commonly you'd see using this method, using it together with alloc and initWithObjectsAndKeys. Now I've always found this format quite clunky and unintuitive because it is written as key value pairs, but it's written as the value-- say iOS SDK New Features--followed by the key which is kind of unusual.
More conventionally across multiple programming languages, it's key then the value. So I am providing four sets of key value pairs here, and then you also have to terminate an NSDictionary with nil. There is going to be a lots of problems if you don't do that. So let me comment that one out and show you the new way of doing it now. Once again using the at sign, but this time we are not using the square brackets, we are using the curly braces, and if you see the pop-up here, I can see the close to this literal syntax.
We've got the NSString is using the double quotes, we've got NSArray is using the square brackets, and we've got NSDictionary is using the curly braces with pairs of keys colon object comma. So we just give it repeating pairs of keys and values. I'll just scroll up to give myself a bit more room here, and I'll split this across lines so it's a little more readable. And this is the new NSDictionary literal format, it's using the key then the value, the more conventional way to write these, and I just provide pairs of these.
All right, so I am just going to paste in a little code here. So I've split these on multiple lines so they are more readable, I am just giving it multiple pairs of keys values, here I am actually using the NSNumber literal syntax to just pass in directly the number 2013 and a Boolean value of YES. We have the closing curly brace, and we finish the line. As with the NSArray literal format, a nil is no longer needed to terminate the NS dictionary. We just finish writing the key value pairs. And also adds with NSArray we have a shorter way of getting to the values inside there.
This is the conventional way of accessing an element in an NSDictionary, we use the value for key method, and this is the new way. The name of the dictionary and while we create it with curly braces, we access the elements using the square brackets, the subscript operator. Same is with an array and same is in many other languages, and I just provide the key to that element, so author for example. And this could be a separate independent NSString object. I am just using NSString literals for the sake of time.
So if I go ahead and run it, well, I see I am actually mismatching my own values here, I am saying the title is but also writing out the author, so let's try and pass in a different key, and that's what I'd expect to see here. So to summarize NSString literal format which you've used before is the at sign and the double quotes, whatever you want inside that. The NSArray is the at sign and the square brackets and your list of objects inside that, the NSDictionary is the at sign and the curly braces, and the NSNumber is just the at sign and the primitive value that you want to box up.
And one thing worth mentioning, when I say they've added the syntax for creating NSArrays and NSDictionaries and NSNumbers, I don't mean those classes and all the other things just like them like NSSets and NSMutableArrays. No, those don't support this format. It is just NSArray, NSDictionary, and NSNumber, at least for now.
- Understanding the impact of changes to Xcode and Objective-C
- Adding and configuring the new UICollectionView
- Dynamically changing the size of collection view cells
- Using Auto Layout to arrange an iOS user interface
- Creating a custom pass with the Passbook API
- Posting to Facebook with the Social framework