Apple Objective-C programming fundamentals. Working with Data, Part 2, Create the data model

In this part of the series we will continue the development of our data store app that we set up in the first part of the series.

Create the Data Model

  • Open your sample application that you created in the first part
  • Open the Project Navigator
  • Select the Supporting Files group
  • Click the plus (+) sign in the lower left corner of the screen
  • Select New File… in the pop-up
  • Under iOS select Core Data on the left side
  • Select the Data Model icon in the middle of the window
  •  Click the Next to save the file

xcode create data model


Add an Entity to the data model

  • Open the .xdatamodeld file in the Project Navigator
  • Click the Add Entity button at the bottom of the screen
    xcode data add entity
  • In the upper right corner of the window under Entities name the entity Event
    xcode data entity name

Add an attribute to the Entity

  • Click the plus (+) sign at the bottom of the Attribute section of the window
    xcode data add attribute
  • Name the attribute creationDate, and set the type to Date in the pop-up menu
    xcode data attribute date
  • Add another attribute, name it as latitude and set the type to Double
  • Add a third attribute, name it longitude and set the type to Double
    As you are adding the new attributes the rows jump around to be displayed alphabetically, so make sure you are setting the type of the correct row.
    The attributes of the Event entity should look like this
    xcode data event attributes

Create a custom class to represent the Event entity to be able to create methods for the entity

  • Select the Event entity on the window
  • In the File menu select New -> New File…
  • In the New File dialog, select NSManagedObject subclass
    xcode data create managed object subclass
  • Click the Next button
  • Save the file in the project folder

Import the new class into the ViewController

  • Add the following to the RootViewController.m implementation file
    #import "Event.h"

Leave a comment

Your email address will not be published. Required fields are marked *