Database Reference
In-Depth Information
that we provide in Interface Builder. In our recipe application, we have one
NSArrayController
that is bound to the
Recipe
entity in Core Data. Because we also
bound that
NSArrayController
to our
NSManagedObjectContext
, it is able to retrieve
those
Recipe
entities automatically and make them available to the rest of the
user interface. When our interface is loaded, those
NSArrayController
objects go
out to that
NSManagedObjectContext
and ask for the entities that currently exist.
Once they are loaded into the
NSArrayController
objects, any view element asso-
ciated with them will be notified, via KVO, that the data is available for display.
All of this happens behind the scenes; we're not required to write code for
any of it.
NSFormatter
Users expect fields in the interface to accept their input and format it appro-
priately. This is where
NSFormatter
objects come into play. When dealing with
any type of number, it is best to add an
NSNumberFormatter
to the text field or
table column and define its display. Likewise, when working with dates, use
an
NSDateFormatter
on the field or column to ensure the data is formatted and
validated correctly before it is stored in the Core Data repository. When
working with Core Data, it is sometimes necessary to manipulate the display
of the data so the user's input can be validated and also so it can be displayed
in a usable form. For instance, we're not creating a very good user experience
if we display currency as 3.99 rather than $3.99 or display a date in raw
seconds.
In our application, we used an
NSNumberFormatter
to display the quantity in the
Ingredients column of our second
NSTableView
. If we were to add a shopping
list to our application, we would also use
NSNumberFormatter
objects to display
currency and
NSDateFormatter
objects to show date and time information.
To add an
NSFormatter
to a field (either a column or a text field), select it in the
Library palette and drag it onto the interface element. Once it is in place, we
can configure its details in the Attributes inspector, as shown in
Figure 29,
NSNumberFormatter Attributes inspector
, on page 147
. The Attributes inspector
allows us to configure exactly how the data is presented to the user.
In addition to properly displaying number and date data, the
NSFormatter
classes accept input from the user and send that input back to the model in
the correct format. For example, by applying
NSNumberFormatter
to the Quantity
column of the ingredients table, we are guaranteed to receive an
NSNumber
back from the user interface.