text
stringlengths
0
13.4k
adding internal behavior to a class without allowing other objects access to it.
For example, say you want to keep a to-do item’s completionDate updated. If the to-do item gets marked
as completed, set completionDate to the current date. If it gets marked as uncompleted, set completionDate
to nil, because it hasn’t been completed yet. Because updating the to-do item’s completionDate is a
self-contained task, the best practice is to write its own method for it. However, it’s important to make sure
that other objects can’t call this method—otherwise, another object could set the to-do item’s completionDate
to anything at any time. For this reason, you make this method private.
Now, update the implementation of XYZToDoItem to include the private method setCompletionDate that
gets called inside markAsCompleted: to update the to-do item’s completionDate whenever it gets marked
as completed or uncompleted. Notice that you’re not adding anything to the interface file, because you don’t
want other objects to see this method.
@implementation XYZToDoItem
- (void)markAsCompleted:(BOOL)isComplete {
self.completed = isComplete;
[self setCompletionDate];
}
- (void)setCompletionDate {
if (self.completed) {
self.completionDate = [NSDate date];
2013-10-22 | Copyright © 2013 Apple Inc. All Rights Reserved.
91
Writing a Custom Class
Methods Define an Object’s Behavior
} else {
self.completionDate = nil;
}
}
@end
At this point, you’ve defined a basic representation of a to-do list item using the XYZToDoItem class.
XYZToDoItem stores information about itself—name, creation date, completion state—in the form of properties,
and it defines what it can do—get marked as completed or uncompleted—using a method. This is the extent
of the features you need to finish implementing your ToDoList app in the next tutorial. However, you can
always experiment by adding your own properties and methods to the class to integrate new behavior into
your app.
2013-10-22 | Copyright © 2013 Apple Inc. All Rights Reserved.
92
Tutorial: Add Data
This tutorial builds on the project you created in the second tutorial (“Tutorial: Storyboards” (page 47)). You’ll
use what you learned about using design patterns, working with Foundation, and writing a custom class to
add support for dynamic data to your ToDoList app.
This tutorial teaches you how to:
● Work with common Foundation classes
Create custom data classes
Implement a delegate and data source protocol
Pass data between view controllers
After you complete all the steps in this tutorial, you’ll have an app that looks something like this:
2013-10-22 | Copyright © 2013 Apple Inc. All Rights Reserved.
93
Tutorial: Add Data
Create a Data Class
Create a Data Class
To get started, open your existing project in Xcode.
At this point, you have an interface and a navigation scheme for your ToDoList app using storyboards. Now,
it’s time to add data storage and behavior with model objects.
The goal of your app is to create a list of to-do items, so first you’ll create a custom class, XYZToDoItem, to
represent an individual to-do item. As you recall, the XYZToDoItem class was discussed in “Writing a Custom
Class” (page 86).
To create the XYZToDoItem class