在上篇文章中,筆者寫了如何建立Core Data項目,以及建立數據模型,沒有閱讀的小夥伴請移步,先了解一下相關內容吧: [ [iOS]Core Data淺析(一) ](https://segmentfault.com/a/11... ;
今天,咱們來討論,如何將建立的可視化模型,轉化爲OC的對象模型.
接着咱們上一個工程繼續操做;segmentfault
來到咱們的LZCoreData.xcdatamodeld文件,選中PeopleEntity實體,而後點擊Xcode導航欄的File-->New -->File...或者直接command+N,新建文件,選擇Core Data-->NSManagedObject subclassoop
Next-->Next.來到以下界面:fetch
這裏會顯示項目中全部待轉化爲模型的實體列表,選擇你要轉化的實體,繼續Next-->Create;
這時,項目的左側導航會多了幾個文件:spa
就是分別爲PeopleEntit和ManEntity建立的模型文件,這裏咱們主要用到的是那四個名稱比較長的類目文件;
這裏有一個問題,須要咱們手動處理一下:代理
比較這兩個類目文件,咱們會發現,在PeopleEntity+CoreDataProperties.h文件中的manRelationship屬性被聲明爲了NSManagedObject類型,而咱們建立的模型中這個屬性類型是ManEntity,這是不對的;而在ManEntity+CoreDataProperties.h文件中的peopleRelationship是PeopleEntity類型的,是正確的;這是由於在生成PeopleEntity的模型的時候ManEntity的模型並不存在,因此係統不知道有這個類型,就直接聲明爲NSManagedObject類型;而在生成ManEntity的模型的時候,PeopleEntity的模型已經存在了,因此他的屬性聲明是正常的;
這時,只須要咱們手動改一下PeopleEntity+CoreDataProperties.h的屬性manRelationship的類型爲ManEntity便可;code
改完後,一編譯發現報錯了,這是由於缺乏對ManEntity的引用,這裏咱們並不須要直接訪問ManEntity的屬性,因此只須要在PeopleEntity.h中加上:對象
@class ManEntity;
再編譯,就沒有問題了;ip
模型轉化完成,就能夠在咱們的代碼中使用了,
來到咱們的ViewController.m
文件,
導入頭文件PeopleEntity+CoreDataProperties.h
和ManEntity+CoreDataProperties.h
,
並修改成如下代碼:rem
//獲取代理 AppDelegate *delegate = [[UIApplication sharedApplication] delegate]; //獲取context NSManagedObjectContext *context = [delegate managedObjectContext]; //獲取PeopleEntity實體 //這裏修改成PeopleEntity類型 PeopleEntity *people = [NSEntityDescription insertNewObjectForEntityForName:@"PeopleEntity" inManagedObjectContext:context]; //設置屬性內容 people.name = @"流火緋瞳"; people.age = @27; people.sex = @0; // [people setValue:@"流火緋瞳" forKey:@"name"]; // [people setValue:@26 forKey:@"age"]; // [people setValue:@0 forKey:@"sex"]; //獲取ManEntit實體 //這裏修改成ManEntity類型 ManEntity *man = [NSEntityDescription insertNewObjectForEntityForName:@"ManEntity" inManagedObjectContext:context]; man.height = @178.0; man.weight = @60.0; man.name = @"張三丰"; man.peopleRelationship = people; // [man setValue:@178.0 forKey:@"height"]; // [man setValue:@60.0 forKey:@"weight"]; // [man setValue:@"張三" forKey:@"name"]; // [man setValue:people forKey:@"peopleRelationship"]; // // [people setValue:man forKey:@"manRelationship"]; people.manRelationship = man; NSError *error; //保存更改 if ([context save:&error]) { NSLog(@"保存成功"); } else { NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]); } //查詢實體 //建立一個查詢請求 NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; //獲取要查詢的實體 NSEntityDescription *entity = [NSEntityDescription entityForName:@"PeopleEntity" inManagedObjectContext:context]; //添加到查詢請求 [fetchRequest setEntity:entity]; //開始查詢並獲取結果 NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error]; NSLog(@"輸出查詢結果"); for (PeopleEntity *info in fetchedObjects) { NSLog(@"Name: %@", info.name); NSLog(@"age: %@", info.age); NSLog(@"sex: %@", info.sex); NSLog(@"-----------------------------------"); // NSLog(@"Name: %@", [info valueForKey:@"name"]); // NSLog(@"age: %@", [info valueForKey:@"age"]); // NSLog(@"sex: %@", [info valueForKey:@"sex"]); // NSLog(@"-----------------------------------"); ManEntity *man1 = [info valueForKey:@"manRelationship"]; NSLog(@"Name: %@", man1.name); NSLog(@"weight: %@", man1.weight); NSLog(@"height: %@", man1.height); NSLog(@"=========================================="); // NSLog(@"Name: %@", [man1 valueForKey:@"name"]); // NSLog(@"weight: %@", [man1 valueForKey:@"weight"]); // NSLog(@"height: %@", [man1 valueForKey:@"height"]); // NSLog(@"=========================================="); }
爲了便於比較先後作了哪些更改,這裏我沒有把原來的KVC模式賦值取值方法刪除,只是註釋掉了;並添加了咱們熟悉的對象屬性賦值取值方式;
運行一下看看結果吧:get
2016-05-26 16:22:27.286 LZCoreData[5765:594436] 保存成功 2016-05-26 16:22:27.288 LZCoreData[5765:594436] 輸出查詢結果 2016-05-26 16:22:27.288 LZCoreData[5765:594436] Name: 流火緋瞳 2016-05-26 16:22:27.288 LZCoreData[5765:594436] age: 26 2016-05-26 16:22:27.288 LZCoreData[5765:594436] sex: 0 2016-05-26 16:22:27.288 LZCoreData[5765:594436] ----------------------------------- 2016-05-26 16:22:27.289 LZCoreData[5765:594436] Name: 張三 2016-05-26 16:22:27.289 LZCoreData[5765:594436] weight: 60 2016-05-26 16:22:27.289 LZCoreData[5765:594436] height: 178 2016-05-26 16:22:27.290 LZCoreData[5765:594436] ========================================== 2016-05-26 16:22:27.290 LZCoreData[5765:594436] Name: 流火緋瞳 2016-05-26 16:22:27.290 LZCoreData[5765:594436] age: 27 2016-05-26 16:22:27.290 LZCoreData[5765:594436] sex: 0 2016-05-26 16:22:27.290 LZCoreData[5765:594436] ----------------------------------- 2016-05-26 16:22:27.290 LZCoreData[5765:594436] Name: 張三丰 2016-05-26 16:22:27.290 LZCoreData[5765:594436] weight: 60 2016-05-26 16:22:27.290 LZCoreData[5765:594436] height: 178 2016-05-26 16:22:27.308 LZCoreData[5765:594436] ==========================================
什麼?有兩組數據!! 若是你是接着上篇文章作的,這裏固然會輸出兩組數據,由於咱們保存了兩次數據嘛.由可視化的模型轉化爲對象模型到這裏就完成了,而後就能夠在項目中使用了.固然,到目前爲止,咱們只是介紹了Core Data的使用準備工做,真正使用的話,咱們須要完善他的增刪改查功能;