CoreData的学习
第一步:创建项目是勾选coredata,当然创建的时候没有勾选,之后还可以手动生产,
然后:创建数据库模型,及为其添加模型的属性。
然后生成模型文件:
注意⚠️:首先设置为Manual/None 不然在编译的过程中也会自动生成模型文件 造成编译失败
模型文件生成后,不用做修改,
生成模型文件后,就可以使用了。
现在就开始上代码:
- #import "ViewController.h"
- #import "AppDelegate.h"
- #import "Person+CoreDataClass.h"
- @interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
- @property (nonatomic, strong) UITableView * tableView;
- @property (nonatomic, strong) NSMutableArray * dataSource;
- @property (nonatomic, strong) AppDelegate * myAppdelegate;
- @end
- @implementation ViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- self.view.backgroundColor = [UIColor whiteColor];
- // 初始化数据源数组
- self.dataSource = [NSMutableArray array];
- self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds];
- self.tableView.delegate = self;
- self.tableView.dataSource = self;
- [self.view addSubview:self.tableView];
- self.tableView.rowHeight = ;
- UIButton *add = [UIButton buttonWithType:UIButtonTypeContactAdd];
- [add addTarget:self action:@selector(clickAdd) forControlEvents:UIControlEventTouchUpInside];
- UIBarButtonItem *rightitem = [[UIBarButtonItem alloc]initWithCustomView:add];
- self.navigationItem.rightBarButtonItem = rightitem;
- AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
- _myAppdelegate = delegate ;
- NSManagedObjectContext *context = _myAppdelegate.persistentContainer.viewContext;
- // 进入控制器时 查询数据库数据
- /**
- 两种方式创建request
- */
- // NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Person"];
- NSFetchRequest *request = [Person fetchRequest];
- /**
- 数据排序
- */
- NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:@"age" ascending:YES];
- request.sortDescriptors = @[descriptor];
- /**
- 刷选数据
- */
- // NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age == 8"];
- // request.predicate = predicate;
- NSError *error = nil;
- NSArray *result = [context executeFetchRequest:request error:&error];
- for (Person * p in result) {
- NSLog(@"-查询结果:-----name:%@-------age:%lld",p.name,p.age);
- }
- [self.dataSource addObjectsFromArray:result];
- [self.tableView reloadData];
- }
- /**
- 插入数据
- */
- - (void)clickAdd
- {
- NSManagedObjectContext *context = _myAppdelegate.persistentContainer.viewContext;
- Person *person = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:context];
- person.name = @"jeck";
- NSInteger temp = random() % + ;
- person.age = temp;
- [self.dataSource addObject:person];
- [self.tableView reloadData];
- [_myAppdelegate saveContext];
- }
- -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- {
- return self.dataSource.count;
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
- if (!cell) {
- cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
- }
- Person *modle = self.dataSource[indexPath.row];
- cell.textLabel.text = [NSString stringWithFormat:@"%@----%lld",modle.name,modle.age];
- return cell;
- }
- - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
- {
- return YES;
- }
- /**
- 删除数据
- */
- - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
- {
- if (editingStyle == UITableViewCellEditingStyleDelete) {
- Person *p = self.dataSource[indexPath.row];
- [self.dataSource removeObject:p];
- [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
- [_myAppdelegate.persistentContainer.viewContext deleteObject:p];
- [_myAppdelegate saveContext];
- }
- }
- /**
- 点击修改数据
- */
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
- {
- Person *p = self.dataSource[indexPath.row];
- p.name = @"rose";
- [_myAppdelegate saveContext];
- [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
- }
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
简单说一下数据库的升级,模型版本的迁移:(能够使老版本的数据库正常使用)
新生成的新数据库文件:
切换之后,文件上勾选的是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的学习的更多相关文章
- iOS CoreData技术学习资源汇总
一.CoreData学习指引 1. 苹果官方:Core Data Programming Guide 什么是CoreData? 创建托管对象模型 初始化Core Data堆栈 提取对象 创建和修改自定 ...
- Core Data (一)备
序 恩,用Core Data也有一段时间了.大大小小的坑也都坑过了.重来没有认真的记录一次.这次需要好好的理一理Core Data.就当一次绝好的机会记录下来.也为了自己加深认识. 为什么要用Core ...
- masonry使用问题
2015年11月3日 coreData的学习练习中复习使用masonry自动布局 masonry自动布局发现问题: 两个控件的相对布局: 如果被参考对象用这个带anchor的属性,就会报这样一个错误: ...
- FireFox插件SQLite Manager的使用
最近几天开始高IOS数据库来着,一开始就CoreData的学习,结果高了一天没有一点进展. 没法,还是先老实代码着吧,不过用的火狐插件可视化数据库的操作也是不错的似乎. FireFox 插件:SQLi ...
- 【原】iOS学习之SQLite和CoreData数据库的比较
1. SQLite数据库 sqlite数据库操作的基本流程是, 创建数据库, 再通过定义一些字段来定义表格结构, 可以利用sql语句向表格中插入记录, 删除记录, 修改记录, 表格之间也可以建立联系. ...
- IOS学习:ios中的数据持久化初级(文件、xml、json、sqlite、CoreData)
IOS学习:ios中的数据持久化初级(文件.xml.json.sqlite.CoreData) 分类: ios开发学习2013-05-30 10:03 2316人阅读 评论(2) 收藏 举报 iOSX ...
- 学习在创建好的工程里面添加CoreData
在学习CoreData中, 在建工程后, 没有添加, 于是就参考网络文章进行更改. 这几天在学习做一个ios的小项目,项目中需要对数据进行基本的增删改查操作.于是就想用一把CoreData.但在创建项 ...
- CoreData学习-最好的一片文章
CoreData学习-最好的一片文章 分类: IOS重新上路2014-05-25 18:00 1937人阅读 评论(0) 收藏 举报 目录(?)[+] 写的很好的一篇教程,我什么时候能写出这么 ...
- CoreData的使用(IOS学习)
——杂言:最近开始学习IOS7的开发,下文是在已经建好的项目里加入CoreData的结构,并实现一个基于coredata的简单save,query. 1. 引入Core Data Framework. ...
随机推荐
- dynamic:动态类型简单用法,写法
class 动态创建数据 { //动态类型:本质感觉跟object的用法差不多,只是在执行的时候才知道数据类型 public dynamic Dynamic() { //定义一个动态类型,作为返回值 ...
- Install ElasticSearch plugin for head
git clone git://github.com/mobz/elasticsearch-head.git yum install git npm cd elasticsearch-head npm ...
- js apply和call
apply()和call()这两个方法的作用是一样的,都是在特定作用域中调用函数,等于设置函数体内this对象的只,以扩充函数赖以运行的作用域 apply:方法能劫持另外一个对象的方法,继承另外一个对 ...
- python csv.reader参数指定
- STM32中管脚利用
如果利用4线SWD则剩余的调试引脚可以作为IO使用: void JTAG_Set(unsigned char Mode){ u32 temp; temp=Mode; temp<<=25; ...
- 错误 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
- [转]JS 只能输入数字和两位小数的JS
本文转自:http://blog.sina.com.cn/s/blog_724008890101dgep.html JS代码: <script language="JavaScript ...
- (转)Shell脚本编程--Uniq命令
uniq 原文:http://blog.csdn.net/xifeijian/article/details/9209627 uniq命令可以去除排序过的文件中的重复行,因此uniq经常和sort合用 ...
- DEDE日期调用小插件
在日期文本框里面,点击的时候,下面出来一个和万年历一样的日期选择表,在dede里面,有一个现成的js小插件,直接调用就OK了... <input type="text" on ...
- 学习JVM-GC原理
1. 前言 Java和C++之间显著的一个区别就是对内存的管理.和C++把内存管理的权利赋予给开发人员的方式不同,Java拥有一套自动的内存回收系统(Garbage Collection,GC)简称G ...