text
stringlengths
0
13.4k
1.
Choose File > New > File (or press Command-N).
A dialog appears that prompts you to choose a template for your new file.
2. On the left, select Cocoa Touch under iOS.
3.
4.
5.
Select Objective-C Class, and click Next.
In the Class field, type ToDoItem after the XYZ prefix.
Choose NSObject from the “Subclass of” pop-up menu.
If you’ve been following along with the tutorials exactly, the Class title probably said
XYZToDoItemViewController prior to this step. When you choose NSObject as the “Subclass of,” Xcode
knows you’re making a normal custom class and removes the ViewController text that it was adding
previously.
6. Click Next.
7.
8.
9.
The save location defaults to your project directory. Leave that as is.
The Group option defaults to your app name, ToDoList. Leave that as is.
The Targets section defaults to having your app selected and the tests for your app unselected. That’s
perfect, so leave that as is.
10. Click Create.
The XYZToDoItem class is straightforward to implement. It has properties for its name, creation date, and
whether the item has been completed. Go ahead and add these properties to the XYZToDoItem class interface.
To configure the XYZToDoItem class
1.
In the project navigator, select XYZToDoItem.h.
2. Add the following properties to the interface so that the declaration looks like this:
2013-10-22 | Copyright © 2013 Apple Inc. All Rights Reserved.
94
Tutorial: Add Data
Load the Data
@interface XYZToDoItem : NSObject
@property NSString *itemName;
@property BOOL completed;
@property (readonly) NSDate *creationDate;
@end
Checkpoint: Build your project by choosing Product > Build (or pressing Command-B). You’re not using your
new class for anything yet, but building it gives the compiler a chance to verify that you haven’t made any
typing mistakes. If you have, fix them by reading through the warnings or errors that the compiler provides,
and then look back over the instructions in this tutorial to make sure everything looks the way it’s described
here.
Load the Data
You now have a class from which you can create and store the data for individual list items. You also need to
keep a list of those items. The natural place to track this is in the XYZToDoListViewController class—view
controllers are responsible for coordinating between the model and the view, so they need a reference to the
model.
The Foundation framework includes a class, NSMutableArray, that works well for tracking lists of items. It’s
important to use a mutable array so that the user can add items to the array. The immutable version, NSArray,
doesn’t allow you to add items to it after it’s initialized.
To use an array you need to both declare it and create it. You do this by allocating and initializing the array.
To allocate and initialize the array
1.
In the project navigator, select XYZToDoListViewController.m.
Because the array of items is an implementation detail of your table view controller, you declare it in the
.m file instead of the .h file. This makes it private to your custom class.
2. Add the following property to the interface category Xcode created in your custom table view controller