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 😉

Example

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

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

DataModel Class

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

Setting.h

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

Setting.m

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

Memo.h

#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;
@end

Memo.m

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

Color.h

#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;
@end

Color.m

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

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