第一步:创建项目是勾选coredata,当然创建的时候没有勾选,之后还可以手动生产,

然后:创建数据库模型,及为其添加模型的属性。

然后生成模型文件:

注意⚠️:首先设置为Manual/None  不然在编译的过程中也会自动生成模型文件 造成编译失败

模型文件生成后,不用做修改,

生成模型文件后,就可以使用了。

现在就开始上代码:

  1. #import "ViewController.h"
  2. #import "AppDelegate.h"
  3. #import "Person+CoreDataClass.h"
  4.  
  5. @interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
  6. @property (nonatomic, strong) UITableView * tableView;
  7. @property (nonatomic, strong) NSMutableArray * dataSource;
  8. @property (nonatomic, strong) AppDelegate * myAppdelegate;
  9.  
  10. @end
  11.  
  12. @implementation ViewController
  13.  
  14. - (void)viewDidLoad {
  15. [super viewDidLoad];
  16. self.view.backgroundColor = [UIColor whiteColor];
  17. // 初始化数据源数组
  18. self.dataSource = [NSMutableArray array];
  19.  
  20. self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds];
  21. self.tableView.delegate = self;
  22. self.tableView.dataSource = self;
  23. [self.view addSubview:self.tableView];
  24. self.tableView.rowHeight = ;
  25.  
  26. UIButton *add = [UIButton buttonWithType:UIButtonTypeContactAdd];
  27. [add addTarget:self action:@selector(clickAdd) forControlEvents:UIControlEventTouchUpInside];
  28. UIBarButtonItem *rightitem = [[UIBarButtonItem alloc]initWithCustomView:add];
  29. self.navigationItem.rightBarButtonItem = rightitem;
  30.  
  31. AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  32. _myAppdelegate = delegate ;
  33. NSManagedObjectContext *context = _myAppdelegate.persistentContainer.viewContext;
  34.  
  35. // 进入控制器时 查询数据库数据
  36. /**
  37. 两种方式创建request
  38. */
  39. // NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Person"];
  40. NSFetchRequest *request = [Person fetchRequest];
  41. /**
  42. 数据排序
  43. */
  44. NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:@"age" ascending:YES];
  45. request.sortDescriptors = @[descriptor];
  46. /**
  47. 刷选数据
  48. */
  49. // NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age == 8"];
  50. // request.predicate = predicate;
  51. NSError *error = nil;
  52. NSArray *result = [context executeFetchRequest:request error:&error];
  53. for (Person * p in result) {
  54. NSLog(@"-查询结果:-----name:%@-------age:%lld",p.name,p.age);
  55. }
  56. [self.dataSource addObjectsFromArray:result];
  57. [self.tableView reloadData];
  58. }
  59.  
  60. /**
  61. 插入数据
  62. */
  63. - (void)clickAdd
  64. {
  65. NSManagedObjectContext *context = _myAppdelegate.persistentContainer.viewContext;
  66. Person *person = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:context];
  67. person.name = @"jeck";
  68. NSInteger temp = random() % + ;
  69. person.age = temp;
  70. [self.dataSource addObject:person];
  71. [self.tableView reloadData];
  72. [_myAppdelegate saveContext];
  73. }
  74.  
  75. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  76. {
  77. return self.dataSource.count;
  78. }
  79.  
  80. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  81. {
  82. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
  83. if (!cell) {
  84. cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
  85. }
  86. Person *modle = self.dataSource[indexPath.row];
  87. cell.textLabel.text = [NSString stringWithFormat:@"%@----%lld",modle.name,modle.age];
  88. return cell;
  89. }
  90.  
  91. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
  92. {
  93. return YES;
  94. }
  95.  
  96. /**
  97. 删除数据
  98. */
  99. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  100. {
  101. if (editingStyle == UITableViewCellEditingStyleDelete) {
  102. Person *p = self.dataSource[indexPath.row];
  103. [self.dataSource removeObject:p];
  104. [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
  105. [_myAppdelegate.persistentContainer.viewContext deleteObject:p];
  106. [_myAppdelegate saveContext];
  107. }
  108. }
  109.  
  110. /**
  111. 点击修改数据
  112. */
  113. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  114. {
  115. Person *p = self.dataSource[indexPath.row];
  116. p.name = @"rose";
  117. [_myAppdelegate saveContext];
  118. [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
  119. }
  120.  
  121. - (void)didReceiveMemoryWarning {
  122. [super didReceiveMemoryWarning];
  123. // Dispose of any resources that can be recreated.
  124. }

简单说一下数据库的升级,模型版本的迁移:(能够使老版本的数据库正常使用)

新生成的新数据库文件:

切换之后,文件上勾选的是test_coreDate2了,此时就可以向新的模型库里添加新增的模型属性了,

然后需要生成一个模型的映射文件:

command + N

之后就会生成一个 v1.0_Tov1.1Model.xcmappingmodel 文件,前面的名字自己随意;

然后,就把旧的数据库的四个模型文件(Person+CoreDataClass.h  ...m   Person+CoreDataProperties.h ....m)删除,重新使用新的数据库test_CoreData2 创建模型文件。

看网上说的还需要修改Appdelegate中的代码,发现现在不用修改了。(想修改也找不到地方了,现在自动生成的代码和之前的不一样了)

在网上很多都是旧的,只发现这个比较新点http://blog.csdn.net/willluckysmile/article/details/76464249

CoreData的学习的更多相关文章

  1. iOS CoreData技术学习资源汇总

    一.CoreData学习指引 1. 苹果官方:Core Data Programming Guide 什么是CoreData? 创建托管对象模型 初始化Core Data堆栈 提取对象 创建和修改自定 ...

  2. Core Data (一)备

    序 恩,用Core Data也有一段时间了.大大小小的坑也都坑过了.重来没有认真的记录一次.这次需要好好的理一理Core Data.就当一次绝好的机会记录下来.也为了自己加深认识. 为什么要用Core ...

  3. masonry使用问题

    2015年11月3日 coreData的学习练习中复习使用masonry自动布局 masonry自动布局发现问题: 两个控件的相对布局: 如果被参考对象用这个带anchor的属性,就会报这样一个错误: ...

  4. FireFox插件SQLite Manager的使用

    最近几天开始高IOS数据库来着,一开始就CoreData的学习,结果高了一天没有一点进展. 没法,还是先老实代码着吧,不过用的火狐插件可视化数据库的操作也是不错的似乎. FireFox 插件:SQLi ...

  5. 【原】iOS学习之SQLite和CoreData数据库的比较

    1. SQLite数据库 sqlite数据库操作的基本流程是, 创建数据库, 再通过定义一些字段来定义表格结构, 可以利用sql语句向表格中插入记录, 删除记录, 修改记录, 表格之间也可以建立联系. ...

  6. IOS学习:ios中的数据持久化初级(文件、xml、json、sqlite、CoreData)

    IOS学习:ios中的数据持久化初级(文件.xml.json.sqlite.CoreData) 分类: ios开发学习2013-05-30 10:03 2316人阅读 评论(2) 收藏 举报 iOSX ...

  7. 学习在创建好的工程里面添加CoreData

    在学习CoreData中, 在建工程后, 没有添加, 于是就参考网络文章进行更改. 这几天在学习做一个ios的小项目,项目中需要对数据进行基本的增删改查操作.于是就想用一把CoreData.但在创建项 ...

  8. CoreData学习-最好的一片文章

    CoreData学习-最好的一片文章 分类: IOS重新上路2014-05-25 18:00 1937人阅读 评论(0) 收藏 举报   目录(?)[+]   写的很好的一篇教程,我什么时候能写出这么 ...

  9. CoreData的使用(IOS学习)

    ——杂言:最近开始学习IOS7的开发,下文是在已经建好的项目里加入CoreData的结构,并实现一个基于coredata的简单save,query. 1. 引入Core Data Framework. ...

随机推荐

  1. dynamic:动态类型简单用法,写法

    class 动态创建数据 { //动态类型:本质感觉跟object的用法差不多,只是在执行的时候才知道数据类型 public dynamic Dynamic() { //定义一个动态类型,作为返回值 ...

  2. Install ElasticSearch plugin for head

    git clone git://github.com/mobz/elasticsearch-head.git yum install git npm cd elasticsearch-head npm ...

  3. js apply和call

    apply()和call()这两个方法的作用是一样的,都是在特定作用域中调用函数,等于设置函数体内this对象的只,以扩充函数赖以运行的作用域 apply:方法能劫持另外一个对象的方法,继承另外一个对 ...

  4. python csv.reader参数指定

  5. STM32中管脚利用

    如果利用4线SWD则剩余的调试引脚可以作为IO使用: void JTAG_Set(unsigned char Mode){ u32 temp; temp=Mode; temp<<=25; ...

  6. 错误 chamfermatching.cpp:969:30: error: the compiler can assume that the address of ‘annotate_img’

    修改 ./build/modules/contrib/CMakeFiles/opencv_contrib.dir/flags.make文件,删掉-Werror=address,然后重新make

  7. [转]JS 只能输入数字和两位小数的JS

    本文转自:http://blog.sina.com.cn/s/blog_724008890101dgep.html JS代码: <script language="JavaScript ...

  8. (转)Shell脚本编程--Uniq命令

    uniq 原文:http://blog.csdn.net/xifeijian/article/details/9209627 uniq命令可以去除排序过的文件中的重复行,因此uniq经常和sort合用 ...

  9. DEDE日期调用小插件

    在日期文本框里面,点击的时候,下面出来一个和万年历一样的日期选择表,在dede里面,有一个现成的js小插件,直接调用就OK了... <input type="text" on ...

  10. 学习JVM-GC原理

    1. 前言 Java和C++之间显著的一个区别就是对内存的管理.和C++把内存管理的权利赋予给开发人员的方式不同,Java拥有一套自动的内存回收系统(Garbage Collection,GC)简称G ...