Join Simon Allardice for an in-depth discussion in this video Object creation, part of iOS App Development Essential Training.
- View Offline
When we write a simple Objective-C statement like this, we end up with a pointer to a newly created NSString object. But, creating strings isn't a great example of the full syntax for creating objects because this is using a shorthand format that Objective-C provides because strings are so common. Usually, there is a little more to object instantiation than this. Now many object-oriented languages use the key word new when creating new objects. Now, we can use new in Objective-C, we usually don't.
Instead, the typical code is something like this. So I'll define a new variable called myDate, it will be a pointer to an NSDate object. NSDate is another class defined by Apple, and always available as it's part of the foundation framework. So this creates a pointer that is empty. It's a null pointer, it's pointing nowhere. We need to actually create the object that this is pointing to. And to create an object to instantiate in Objective-C, we go through two explicit stages. The first stage is allocation, calling the alloc method here of the NSDate class.
And what this means, as Objective-C will go out to that area of memory that is needed, carve out a space big enough to hold this new object, and return the address and memory of that new object. So our pointer variable now points to an object. But the second stage is initialization. It's going to call the init method of that new object, which should go through it and initialize its internal state, internal data, perform any behaviors it needs to do. Now, initialization could be very simple, it could be very complex.
It depends on the class definition. Although, because it's common in Objective-C to call both alloc and then init, you don't typically spread it across two lines, or even three, in this case. You don't have to. You can nest one inside the other. So, instead of this being written as three separate statements, it's usually written something like this. A nested call first to the alloc method of the NSDate class, and then whatever's returned by that has init called on that, and we get the result stored in our new variable.
This is the most common way that you will see object instantiation in Objective-C. You'll see it all over the place used for all kinds of different classes, just alloc and init, alloc and init. However, often when you're creating an object you don't just have this one standard init method, you'll have a choice of several different versions of init. Different initializer methods that all begin with init. So creating a person object, we might have a version of init that's init with name, meaning we can initialize by passing in a specific piece of data.
If we're creating a new NSString object, there's actually several versions of init, including init with contents of file. There's init with contents of URL, there's init with a CString. There's a whole bunch of different ways to initialize an NSString object. With NSDate, there is an initWithTimeIntervalSince1970 that just expects a number of seconds to be passed in. So there's often multiple ways to control the allocation, and then the specialized initialization of an object, apart from just alloc and init.
But before IOS5, instantiating a new object, allocating and initializing it, was only half of the picture. You also had to be very explicit about when you were finished with that object, so that the memory could be reused and reclaimed. You had to issue retain and release calls on that object. But with the arrival of automatic reference counting, we don't have to worry about that. We'll talk about that next.
- Using Xcode and the iOS Simulator
- Learning Objective-C basics and structure
- Creating objects, variables, properties, and custom classes
- Connecting UI elements to code
- Using delegation
- Using the Xcode debugger
- Creating and customizing table views
- Exploring storyboards
- Introducing blocks
- Saving and loading data
- Understanding the differences in iPad development
- Creating iPad apps with popovers and split views
- Adding application icons and launch images