1.在控制器下導(dǎo)入"AppDelegate.h"頭文件
2.因?yàn)樾枰啻问褂玫紸ppDelegate中的數(shù)據(jù)上下文,為了避免每次都通過(guò)這種方式去獲取:
((AppDelegate *)[UIApplication sharedApplication].delegate).managedObjectModel
所以直接聲明一個(gè)全局成員變量,并通過(guò)懶加載方式獲取
@property (nonatomic,strong) AppDelegate *appDelegate;
- (AppDelegate *)appDelegate{
if (_appDelegate == nil) {
_appDelegate = [UIApplication sharedApplication].delegate;
}
return _appDelegate;
}
在xcdatamodeld文件中,創(chuàng)建實(shí)體,添加字段:

3.接下來(lái)封裝一個(gè)方法,用來(lái)演示插入數(shù)據(jù):
// 插入數(shù)據(jù)
- (void)insertData{
// 設(shè)置實(shí)體 entityForName(相當(dāng)于表名)
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:self.appDelegate.managedObjectContext];
// 創(chuàng)建并且插入的數(shù)據(jù) 對(duì)應(yīng)數(shù)據(jù)庫(kù)中的一條記錄 基類NSManagerObject
NSManagedObject *person = [[NSManagedObject alloc]initWithEntity:entityDescription insertIntoManagedObjectContext:self.appDelegate.managedObjectContext];
// 插入數(shù)據(jù) 如果需要插入的數(shù)據(jù)上下文對(duì)象還不確定,上面設(shè)置實(shí)體,插入數(shù)據(jù)時(shí),上下文可以傳nil,再通過(guò)下面的方法分開(kāi)使用
// self.appDelegate.managedObjectContext insertObject:<#(nonnull NSManagedObject *)#>
// 設(shè)置數(shù)據(jù)
[person setValue:@"laowang" forKey:@"name"];
[person setValue:@18 forKey:@"age"];
[person setValue:@178.5 forKey:@"height"];
// 保存上下文 保存后才會(huì)和數(shù)據(jù)庫(kù)進(jìn)行同步
[self.appDelegate saveContext];
}
4.在ViewDidLoad中調(diào)用插入數(shù)據(jù)方法后,通過(guò)導(dǎo)入CoreData類庫(kù)默認(rèn)提供的獲取沙盒方法,打印出沙盒路徑,查看沙盒路徑

第一個(gè)就是我們生成的CoreData數(shù)據(jù)庫(kù)文件
后面兩個(gè)文件是用來(lái)做數(shù)據(jù)回滾的
這樣插入數(shù)據(jù)就完成了
上面的示例代碼中,在設(shè)置數(shù)據(jù)時(shí),使用的是KVC的方式,但在現(xiàn)實(shí)開(kāi)發(fā)中,數(shù)據(jù)庫(kù)表的字段可能會(huì)很多,即便字段不是很多,這樣設(shè)置起來(lái)也比較麻煩
我們可以通過(guò)Xcode給NSManagedObject自動(dòng)生成一個(gè)子類,直接以屬性的方式來(lái)使用,自動(dòng)生成子類方式來(lái)實(shí)現(xiàn),具體步驟:
1> 創(chuàng)建并且插入數(shù)據(jù)對(duì)象 對(duì)應(yīng)數(shù)據(jù)庫(kù)中的一條記錄 基類NSManagedObject
2> Xcode-Editor設(shè)置NSManagedObject的子類
3> 通過(guò)點(diǎn)語(yǔ)法直接設(shè)置數(shù)據(jù)
4> 保存上下文 保存后才會(huì)和數(shù)據(jù)庫(kù)進(jìn)行同步
首先,通過(guò)Xcode自動(dòng)幫我們生成數(shù)據(jù)模型的子類

點(diǎn)擊"Create NSManagedObject Subclass"后,選擇數(shù)據(jù)模型

接下來(lái)選擇實(shí)體

最后選擇路徑.
自動(dòng)生成的子類Person.h文件
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
NS_ASSUME_NONNULL_BEGIN
@interface Person : NSManagedObject
// Insert code here to declare functionality of your managed object subclass
@end
NS_ASSUME_NONNULL_END
#import "Person+CoreDataProperties.h"

XCode幫我們生成的子類中,并沒(méi)有提供成員變量,而是以分類的形式提供
因?yàn)榉诸愔刑砑訉傩灾皇锹暶鱯etter和getter方法,并不會(huì)生成帶下劃線的成員變量,這里只是通過(guò)@dynamic告訴編譯器,屬性會(huì)在運(yùn)行時(shí)實(shí)現(xiàn)setter和getter方法,CoreData會(huì)在運(yùn)行時(shí)將數(shù)據(jù)對(duì)象的屬性生成setter和getter方法
(并不一定會(huì)生成成員變量,只是可以在外界調(diào)用屬性,只是編譯器不再提示警報(bào))
#import "Person+CoreDataProperties.h"
@implementation Person (CoreDataProperties)
@dynamic age;
@dynamic height;
@dynamic name;
@end
這里是CoreData通過(guò)@dynamic的方式運(yùn)行時(shí)合成了setter和getter方法,并不代表自己可以這樣的方式來(lái)實(shí)現(xiàn)
因?yàn)镻erson中已經(jīng)導(dǎo)入了分類,所以在控制器下使用時(shí),只需要再倒入Person類的頭文件即可
示例代碼:
// 通過(guò)數(shù)據(jù)模型子類
- (void)insertDataWithSubclass{
// 設(shè)置實(shí)體 entityForName (相當(dāng)于表名)
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:self.appDelegate.managedObjectContext];
// 創(chuàng)建并且插入的數(shù)據(jù)
Person *person = [[Person alloc] initWithEntity:entityDescription insertIntoManagedObjectContext:self.appDelegate.managedObjectContext];
// 設(shè)置數(shù)據(jù)
person.name = @"laozhang";
person.age = @30;
person.height = @158.2;
// 保存上下文
[self.appDelegate saveContext];
}
這樣在設(shè)置數(shù)據(jù)時(shí),就通過(guò)點(diǎn)語(yǔ)法直接進(jìn)行賦值實(shí)現(xiàn)了
完整示例代碼:
#import "ViewController.h"
#import "AppDelegate.h"
#import "Person.h"
@interface ViewController ()
@property (nonatomic,strong) AppDelegate *appDelegate;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// [self insertData];
[self insertDataWithSubclass];
// 打印沙盒路徑
NSLog(@"%@",[self.appDelegate applicationDocumentsDirectory]);
}
// 插入數(shù)據(jù)
- (void)insertData{
// 設(shè)置實(shí)體 entityForName(相當(dāng)于表名)
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:self.appDelegate.managedObjectContext];
// 創(chuàng)建并且插入的數(shù)據(jù) 對(duì)應(yīng)數(shù)據(jù)庫(kù)中的一條記錄 基類NSManagerObject
NSManagedObject *person = [[NSManagedObject alloc]initWithEntity:entityDescription insertIntoManagedObjectContext:self.appDelegate.managedObjectContext];
// 插入數(shù)據(jù) 如果需要插入的數(shù)據(jù)上下文對(duì)象還不確定,上面設(shè)置實(shí)體,插入數(shù)據(jù)時(shí),上下文可以傳nil,再通過(guò)下面的方法分開(kāi)使用
// self.appDelegate.managedObjectContext insertObject:<#(nonnull NSManagedObject *)#>
// 設(shè)置數(shù)據(jù)
[person setValue:@"laowang" forKey:@"name"];
[person setValue:@18 forKey:@"age"];
[person setValue:@178.5 forKey:@"height"];
// 如果字段比較多,通過(guò)KVC的方式來(lái)設(shè)置數(shù)據(jù)顯然是不方便的
// 直接
// 保存上下文 保存后才會(huì)和數(shù)據(jù)庫(kù)進(jìn)行同步
[self.appDelegate saveContext];
}
// 通過(guò)數(shù)據(jù)模型子類
- (void)insertDataWithSubclass{
// 設(shè)置實(shí)體 entityForName (相當(dāng)于表名)
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:self.appDelegate.managedObjectContext];
// 創(chuàng)建并且插入的數(shù)據(jù)
Person *person = [[Person alloc] initWithEntity:entityDescription insertIntoManagedObjectContext:self.appDelegate.managedObjectContext];
// 設(shè)置數(shù)據(jù)
person.name = @"laozhang";
person.age = @30;
person.height = @158.2;
// 保存上下文
[self.appDelegate saveContext];
}
- (AppDelegate *)appDelegate{
// ((AppDelegate *)[UIApplication sharedApplication].delegate).managedObjectModel
if (_appDelegate == nil) {
_appDelegate = [UIApplication sharedApplication].delegate;
}
return _appDelegate;
}
@end