數(shù)據(jù)庫(kù)-CoreData插入數(shù)據(jù) - (Obj-C)

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í)體,添加字段:


插入輸入_1.png

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)提供的獲取沙盒方法,打印出沙盒路徑,查看沙盒路徑

沙盒路徑.png

第一個(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ù)模型的子類


CreatNSManagedObjectSubclass.png

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

選擇數(shù)據(jù)模型.png

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

選擇實(shí)體.png

最后選擇路徑.

自動(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"
插入數(shù)據(jù)_2.png

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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • *面試心聲:其實(shí)這些題本人都沒(méi)怎么背,但是在上海 兩周半 面了大約10家 收到差不多3個(gè)offer,總結(jié)起來(lái)就是把...
    Dove_iOS閱讀 27,628評(píng)論 30 472
  • 序言 目前形勢(shì),參加到iOS隊(duì)伍的人是越來(lái)越多,甚至已經(jīng)到供過(guò)于求了。今年,找過(guò)工作人可能會(huì)更深刻地體會(huì)到今年的就...
    Jack_lin閱讀 78,995評(píng)論 110 1,946
  • OC的理解與特性 OC作為一門面向?qū)ο蟮恼Z(yǔ)言,自然具有面向?qū)ο蟮恼Z(yǔ)言特性:封裝、繼承、多態(tài)。它既具有靜態(tài)語(yǔ)言的特性...
    失憶的程序員閱讀 534評(píng)論 0 1
  • 序言 目前形勢(shì),參加到iOS隊(duì)伍的人是越來(lái)越多,甚至已經(jīng)到供過(guò)于求了。今年,找過(guò)工作人可能會(huì)更深刻地體會(huì)到今年的就...
    iOS_Alex閱讀 1,657評(píng)論 1 24
  • 遇到問(wèn)題,你怎么解決?問(wèn)人還是直接谷歌? 先說(shuō)兩件小事兒吧。 昨天我們字幕組的Joy在群里問(wèn):“yihan,這兩天...
    MissYihan閱讀 1,626評(píng)論 14 19

友情鏈接更多精彩內(nèi)容