CoreData(File xcdatamodeld)

Next step is to create data file(.xcdatamodeld).
This is CoreData definition file which includes data type, relationships.

Create data model file(.xcdatamodeld)

To use CoreData, we need definition file.
We describe data model name, data type, relationship here.
“File” -> “New” -> “File” -> “Core Data” -> “Data Model”

Core Data Data Model file

After that, we can create .xcadatamodeld file under our project.

Next, we edit .xcadatamodeld file. There are steps

  • Add Entity
  • Add Attribute
  • Add Relationships(optional)
  • Add Fetched Properties

ENTITIES are model name(finally, this name become class name)
Attirubtes are data name name data type(this is same as data base)
Relationships are relationship between ENTITIES, parent-child, one-to-one etc…

Data Type

  • Undefined
  • Integer 16
  • Integer 32
  • Integer 64
  • Decimal
  • Double
  • Float
  • String
  • Boolean
  • Date
  • Binary Data
  • Transformable

See, we can use Date. SQLite3 doesn’t have Date type by default.
Core Data wrapped that data type 😉


This is example of my software(Colormemo).
CoreData Setting
Core Data Memo
Core Data Color

You can also see diagram.(To push Editor Style switcher)

DataModel Class

After finishing model file, Modal classes are generated automatically.
These are examples.


@interface Setting : NSManagedObject
@property (nonatomic, retain) NSNumber * number;
@property (nonatomic, retain) NSString * key;
@property (nonatomic, retain) NSNumber * value;


@implementation Setting
@dynamic number;
@dynamic key;
@dynamic value;


#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
@class Color;
@interface Memo : NSManagedObject
@property (nonatomic, retain) NSString * uid;
@property (nonatomic, retain) NSString * body;
@property (nonatomic, retain) NSDate * lastmodified;
@property (nonatomic, retain) Color *color;


#import "Memo.h"
#import "Color.h"
@implementation Memo
@dynamic uid;
@dynamic body;
@dynamic lastmodified;
@dynamic color;


#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
@interface Color : NSManagedObject
@property (nonatomic, retain) NSString * key;
@property (nonatomic, retain) NSNumber * number;
@property (nonatomic, retain) NSNumber * r;
@property (nonatomic, retain) NSNumber * g;
@property (nonatomic, retain) NSNumber * b;
@property (nonatomic, retain) NSNumber * alpha;


#import "Color.h"
@implementation Color
@dynamic key;
@dynamic number;
@dynamic r;
@dynamic g;
@dynamic b;
@dynamic alpha;

Modal part was done. Next is Data Access Preparation in AppDelegate.