1.首先创建一个新的工程

记得勾选下面的 Use Core Data

万恶分割线————————————————————————

然后点击Add Entity 创建一个类似于表名。

万恶分割线————————————————————————

然后创建属性名 ,  选择属性的类型

万恶分割线————————————————————————

然后你写好属性之后,就可以让系统帮你生成类了,点击倒数第三个(Create NSManagedObject Subclass...)

万恶分割线————————————————————————

记得,你要用就要勾选它,然后点击下一步。

万恶分割线————————————————————————

然后系统就帮你生成了类。

然后这个时候你可以进到AppDElegate.h

#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window;
//被管理对象上下文(数据管理器)相当于一个临时数据库
@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
//被管理对象模型(数据模型器)
@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
//持久化存储助理(数据连接器)整个CoreData框架中的核心
@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;
//把骂我呢临时数据库中进行的改变进行永久保存
- (void)saveContext;
//获取真实文件的存储路径,NURL类型
- (NSURL *)applicationDocumentsDirectory;

在你ViewController.m文件里面就可以实现数据库的增删改, 注意!!!!! 我的tableview,是拖拽控件的,但是里面的方法几乎都可以借鉴。

#import "AppDelegate.h"
#import "Clothes.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *TableVire;
@property(strong,nonatomic)NSMutableArray *dataSource; //声明一个AppDelegate对象属性,用来调用类中的属性,比如被管理对象上下文
@property(strong,nonatomic)AppDelegate *myAppDelegate; @end @implementation ViewController
/**
* 插入数据
*
* @param sender +barButtonItem
*/
- (IBAction)addModel:(id)sender
{
//插入数据
//创建实体描述对象
NSEntityDescription *description=[NSEntityDescription entityForName:@"Clothes" inManagedObjectContext:self.myAppDelegate.managedObjectContext]; //1.创建一个模型对象
Clothes *cloth=[[Clothes alloc] initWithEntity:description insertIntoManagedObjectContext:self.myAppDelegate.managedObjectContext];
cloth.name=@"Puma";
int price=arc4random()%1000+1;
cloth.price=[NSNumber numberWithInt:price];
//插入数据源数组
[self.dataSource addObject:cloth];
//插入UI
[self.TableVire insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:self.dataSource.count-1 inSection: 0]] withRowAnimation:UITableViewRowAnimationLeft];
//对数据管理器中的更改进行永久存储
[self.myAppDelegate saveContext];
} - (void)viewDidLoad {
[super viewDidLoad];
//初始化数组
self.dataSource=[NSMutableArray array];
self.myAppDelegate=[UIApplication sharedApplication].delegate;
//查询数据
//1.NSFetchRequest对象
NSFetchRequest *request=[[NSFetchRequest alloc] initWithEntityName:@"Clothes"];
//2.设置排序
//2.1设置排序描述对象
NSSortDescriptor *sortDescriptor=[[NSSortDescriptor alloc] initWithKey:@"price" ascending:YES];
request.sortDescriptors=@[sortDescriptor];
//执行这个查询请求
NSError *error=nil;
NSArray *resut= [self.myAppDelegate.managedObjectContext executeFetchRequest:request error:&error];
//给数据源组中添加数据
[self.dataSource addObjectsFromArray:resut]; }
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.dataSource.count;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
Clothes *cloth=self.dataSource[indexPath.row];
cell.textLabel.text=[NSString stringWithFormat:@"%@---%@",cloth.name,cloth.price]; return cell;
}
//允许tableview编辑
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
//tableView编辑的方法
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle==UITableViewCellEditingStyleDelete) {
// 删除数据源
Clothes *cloth=self.dataSource[indexPath.row];
[self.dataSource removeObject:cloth];
//删除数据管理器中的数据
[self.myAppDelegate.managedObjectContext deleteObject:cloth];
//将进行的更改进行永久保存
[self.myAppDelegate saveContext];
//删除单元格
[self.TableVire deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; }
}
//点击cell进行修改数据
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//1.找到模型对象
Clothes *cloth=self.dataSource[indexPath.row];
cloth.name=@"Nike";
// 舒心单元格
[self.TableVire reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
//通过saveContext对数据惊醒永久保存
[self.myAppDelegate saveContext]; } - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end

这样你就对tableview进行增删改,查看, 并且数据会永久存储在你的本地数据库 , 当然这个是很基本,很简单的。

CoreData的一些简单运用的更多相关文章

  1. Coredata 单表简单使用

    ** 使用Coredata 工程中的DataModel创建:系统创建.手动创建** ** 使用Coredata需要要导入<CoreData/CoreData.h> ** 1.系统创建(系统 ...

  2. CoreData数据库浅析

    Core Data是iOS5之后才出现的一个框架,它提供了对象-关系映射(ORM)的功能,即能够将OC对象转化成数据,保存在SQLite数据库文件中,也能够将保存在数据库中的数据还原成OC对象.在此数 ...

  3. 数据处理之CoreData

    一.CoreData数据库框架与Sqlite对比 Sqlite: 1.基于C接口, 需要使用sql语句, 代码繁琐 2.在处理大量数据时, 表关系更直观 3.在OC中不是可视化的 CoreData: ...

  4. CoreData数据库

        一  CoreData 了解 1 CoreData 数据持久化框架是 Cocoa API 的一部分,首先在iOSS5 版本的系统中出现:      它允许按照 实体-属性-值 模式组织数据: ...

  5. iOS学习37数据处理之CoreData

    1. CoreData数据库框架的优势 1> CoreData历史 CoreData数据持久化框架是Cocoa API 的一部分,首次在iOS5版本的系统中出现,它允许按照实体-属性-值模型组织 ...

  6. iOS进阶学习-CoreData

    一.CoreData数据库框架的优势 1.CoreData数据持久化框架是Cocoa API的一部分,首次在iOS5版本的系统中出现,它允许按照实体-属性-值模型组织数据,并以XML.二进制文件或者S ...

  7. 对FMDB的封装JRDB

    在自己开发中,每次用到数据库都会纠结是使用CoreData还是FMDB.CoreData虽然Api简单,但是调用栈非常复杂,要初始化一个Context需要至少20行代码.显然,对于这种这么恶心的情况, ...

  8. ios实例开发精品文章推荐(8.13)

    提示用户对产品进行评价 http://www.apkbus.com/android-137752-1-1.html设置UILabel和UITextField的Insets http://www.apk ...

  9. 【CoreData】 简单地使用

    先介绍一下什么是CoreData —— 它是在iOS5之后出现的一个框架,提供了对象-关系映射(ORM)的功能,既能够将OC对象转化成数据,保存在SQLite数据库文件中,也能将保存在数据库中的数据还 ...

随机推荐

  1. HTML5 Audio/Video 标签,属性,方法,事件汇总

    HTML5 Audio/Video 标签,属性,方法,事件汇总 (转) 2011-06-28 13:16:48   <audio> 标签属性:src:音乐的URLpreload:预加载au ...

  2. 云计算之路-阿里云上:从ASP.NET线程角度对“黑色30秒”问题的全新分析

    在这篇博文中,我们抛开对阿里云的怀疑,完全从ASP.NET的角度进行分析,看能不能找到针对问题现象的更合理的解释. “黑色30秒”问题现象的主要特征是:排队的请求(Requests Queued)突增 ...

  3. swift中Range的使用书名

    在swift中Range有两种用法 1.把字符串转换成NSString来使用 //这里是把swift的字符换转换成了nsstring 使用 let str :NSString = text.strin ...

  4. WIFI网络操作

    WIFI网卡状态(不可用状态值为1,正在关闭值为0,可用状态值为3,正在打开值为2) WIFI网卡状态是由一系列的整型常量表示,这一系列的整型常量都存储于WifiManager的类中 1.WIFI_S ...

  5. js面试题之数组去重对比

    最近看一些面试题,很多都提到了数组去重,用的最多的不外乎就是下面这个例子 arr.filter(function(value,index,arr){ return arr.indexOf(value, ...

  6. 在Mac上使用Visual Studio Code开发/调试.NET Core代码

    .Net Core 1.0终于发布了,Core的一大卖点就是跨平台.这个跨平台不只是跨平台运行,而且可以跨平台开发.今天抽空研究了下在Mac下如何使用VS Code来开发.NET Core程序,并且调 ...

  7. AngularJS中的方法参数的问题

    在使用AngularJS开发的过程中出现了如下的问题,一次贴记录下. 感觉也不能说是AngularJS的语法,应该说是JS里面的处理流程应该就是这样子,我现在想通过前端页面传递值到后端(通过方法传递) ...

  8. processModel与ASP.NET进程模型

    配置 Microsoft Internet 信息服务 (IIS) Web 服务器上的 ASP.NET 进程模型设置.其作用是配置IIS或IIS中的应用程序池(IIS7及以后版本)的安全性,性能,健壮性 ...

  9. 1、怎样设置C#OpenFileDialog(文件选择窗体)的指定路径、文件格式等属性(设置打开默认路径、文件格式、窗体显示文本)

    C#的OpenFileDialog的常用属性设置 1.设置属性 1)设置弹出的指定路径(绝对路径.相等路径) 2)设置标题 3)设置文本格式 2.打开方式1(绝对路径) 2.1) 打开的路径

  10. PHP json字符串,格式化缩进显示

    PHP json字符串,格式化显示 /** * 格式化 */ class JsonFormatHelper { /** * json字符串缩进显示 * @param unknown $json * @ ...