国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

oreData的學習記錄

2019-11-14 20:36:20
字體:
來源:轉載
供稿:網友

1.如果想創建一個帶有coreData的程序,要在項目初始化的時候勾選中

 

2.創建完成之后,會發現在AppDelegate里多出了幾個屬性,和2個方法 

  1. <span style="font-size:18px;">  
  2.   
  3. @PRoperty (readonlystrongnonatomicNSManagedObjectContext *managedObjectContext;  
  4. @property (readonlystrongnonatomicNSManagedObjectModel *managedObjectModel;  
  5. @property (readonlystrongnonatomicNSPersistentStoreCoordinator *persistentStoreCoordinator;  
  6.   
  7. - (void)saveContext;  
  8. - (NSURL *)applicationDocumentsDirectory;</span>  

 

 

managedObjectContext (被管理的數據上下文)操作實際內容(操作持久層)作用:插入數據,查詢數據,刪除數據

 

NSManagedObjectModel(被管理的數據模型)數據庫所有表格或數據結構,包含各實體的定義信息 作用:添加實體的屬性,建立屬性之間的關系操作方法:視圖編輯器,或代碼

NSPersistentStoreCoordinator(持久化存儲助理)相當于數據庫的連接器 作用:設置數據存儲的名字,位置,存儲方式,和存儲時機

方法saveContext表示:保存數據到持久層(數據庫)

方法applicationDocumentsDirectory表示:應用程序沙箱下的Documents目錄路徑

 

3.如果想創建一個實體對象的話,需要點擊.xcdatamodel,Add Entity,添加想要的字段


 

4.生成對象文件,command+n,然后選中CoreData里的NSManagerObjectSubClass進行關聯,選中實體創建

 

5.添加數據 

  1. Person *newPerson = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:self.managedObjectContext];  
  2.       
  3.     if (newPerson == nil){  
  4.         NSLog(@"Failed to create the new person.");  
  5.         return NO;  
  6.     }  
  7.       
  8.     newPerson.firstName = paramFirstName;  
  9.     newPerson.lastName = paramLastName;  
  10.     newPerson.age = [NSNumber numberWithUnsignedInteger:paramAge];  
  11.     NSError *savingError = nil;  
  12.       
  13.     if ([self.managedObjectContext save:&savingError]){  
  14.         return YES;  
  15.     } else {  
  16.         NSLog(@"Failed to save the new person. Error = %@", savingError);  
  17.     }  

 

 

NSEntityDescription(實體結構)相當于表格結構

 

 

6.取出數據查詢 

  1. /* Create the fetch request first */  
  2.     NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];  
  3.     /* Here is the entity whose contents we want to read */  
  4.     NSEntityDescription *entity =  
  5.     [NSEntityDescription  
  6.      entityForName:@"Person"  
  7.      inManagedObjectContext:self.managedObjectContext];  
  8.     /* Tell the request that we want to read the 
  9.      contents of the Person entity */  
  10.     [fetchRequest setEntity:entity];  
  11.     NSError *requestError = nil;  
  12.     /* And execute the fetch request on the context */  
  13.     NSArray *persons =  
  14.     [self.managedObjectContext executeFetchRequest:fetchRequest  
  15.                                              error:&requestError];  
  16.     /* Make sure we get the array */  
  17.     if ([persons count] > 0){  
  18.         /* Go through the persons array one by one */  
  19.         NSUInteger counter = 1;  
  20.         for (Person *thisPerson in persons){  
  21.             NSLog(@"Person %lu First Name = %@",  
  22.                   (unsigned long)counter,   
  23.                   thisPerson.firstName);   
  24.             NSLog(@"Person %lu Last Name = %@",   
  25.                   (unsigned long)counter,   
  26.                   thisPerson.lastName);  
  27.             NSLog(@"Person %lu Age = %ld",  
  28.                   (unsigned long)counter,  
  29.                   (unsigned long)[thisPerson.age unsignedIntegerValue]);  
  30.             counter++;  
  31.         }  
  32.     } else {  
  33.         NSLog(@"Could not find any Person entities in the context.");   
  34.     }  

7.刪除數據 
  1. /* Create the fetch request first */  
  2.     NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];  
  3.     /* Here is the entity whose contents we want to read */  
  4.     NSEntityDescription *entity =  
  5.     [NSEntityDescription  
  6.      entityForName:@"Person"  
  7.      inManagedObjectContext:self.managedObjectContext];  
  8.     /* Tell the request that we want to read the 
  9.      contents of the Person entity */  
  10.     [fetchRequest setEntity:entity];  
  11.     NSError *requestError = nil;  
  12.     /* And execute the fetch request on the context */  
  13.     NSArray *persons =  
  14.     [self.managedObjectContext executeFetchRequest:fetchRequest  
  15.                                              error:&requestError];  
  16.     if ([persons count] > 0){  
  17.         /* Delete the last person in the array */  
  18.         Person *lastPerson = [persons lastObject];  
  19.         [self.managedObjectContext deleteObject:lastPerson];  
  20.         NSError *savingError = nil;  
  21.         if ([self.managedObjectContext save:&savingError]){  
  22.             NSLog(@"Successfully deleted the last person in the array.");  
  23.         } else {  
  24.             NSLog(@"Failed to delete the last person in the array.");  
  25.         }  
  26.     } else {   
  27.         NSLog(@"Could not find any Person entities in the context.");   
  28.     }   

8.排序 
  1. <pre code_snippet_id="243955" snippet_file_name="blog_20140319_5_4289257" name="code" class="objc">NSSortDescriptor *ageSort =   
  2. [[NSSortDescriptor alloc] initWithKey:@"age"   
  3. ascending:YES];   
  4. NSSortDescriptor *firstNameSort =   
  5. [[NSSortDescriptor alloc] initWithKey:@"firstName"   
  6. ascending:YES];   
  7. NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:   
  8. ageSort,   
  9. firstNameSort, nil nil];   
  10. fetchRequest.sortDescriptors = sortDescriptors; </pre><p></p>  
  11. <pre></pre>  
  12. <p></p>  
  13. <p style="background-color:rgb(255,255,255); margin:10px auto; padding-top:0px; padding-bottom:0px; font-family:verdana,'ms song',Arial,Helvetica,sans-serif; line-height:19.09090805053711px">  
  14. <span style="background-color:rgb(255,255,255)"><span style="font-size:18px"><br>  
  15. </span></span></p>  
  16. <span style="background-color:rgb(255,255,255)">注意</span><span style="font-size:18px; background-color:rgb(255,255,255)">ascending:YES 屬性決定排序順序</span><span style="background-color:rgb(255,255,255)"><span style="font-size:18px"><br>  
  17. <br>  
  18. <br>  
  19. </span></span><br>  
 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 辽源市| 阳东县| 邢台市| 香港 | 浙江省| 安阳县| 天镇县| 商南县| 三门县| 贡嘎县| 中宁县| 江源县| 吐鲁番市| 当涂县| 武夷山市| 新泰市| 姜堰市| 玛纳斯县| 梁平县| 宜黄县| 奎屯市| 新乡县| 汝南县| 武安市| 绥棱县| 云梦县| 视频| 英超| 晋宁县| 明星| 广丰县| 临漳县| 安化县| 田东县| 汶川县| 逊克县| 九龙城区| 登封市| 水城县| 吕梁市| 右玉县|