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. C算法编程题(五)“E”的变换

    前言 上一篇<C算法编程题(四)上三角> 插几句话,说说最近自己的状态,人家都说程序员经常失眠什么的,但是这几个月来,我从没有失眠过,当然是过了分手那段时期.每天的工作很忙,一个任务接一个 ...

  2. react-native 学习之TextInput组件篇

    /** * Sample React Native App * https://github.com/facebook/react-native */ 'use strict'; import Rea ...

  3. 开启SharePoint Server 2013 中的“微博”功能——新闻源

    熟悉SharePoint的朋友在2013之前的版本可以使用社区协作下的记事板.应用程序下的通知,来进行消息的发布,而且更有这两者的完美结合体讨论板,可供使用着根据站点属性进行添加而对现在的快消息时代, ...

  4. Android 程序打包及签名

    为什么要签名??? 开发Android的人这么多,完全有可能大家都把类名,包名起成了一个同样的名字,这时候如何区分?签名这时候就是起区分作用的. 由于开发商可能通过使用相同的Package Name来 ...

  5. Android系统build.prop文件

    # begin build properties (开始设置系统性能) # autogenerated by buildinfo.sh (通过设置形成系统信息) ro.build.id=GRI40 ( ...

  6. 分享在winform下实现左右布局多窗口界面-续篇

    之前的这篇文章<分享在winform下实现左右布局多窗口界面>已经实现了左右布局多窗口界面,今天本来是研究基于winform的插件编程,没想到顺便又找到了另一种实现方案,这种实现方案更简单 ...

  7. ES6笔记系列

    ES6,即ECMAScript 6.0,ES6的第一个版本是在2015年发布的,所以又称作ECMAScript 2015 如今距ES6发布已经一年多的时间,这时候才去学,作为一个JSer,羞愧羞愧,还 ...

  8. ASP.NET Core开发-后台任务利器Hangfire使用

    ASP.NET Core开发系列之后台任务利器Hangfire 使用. Hangfire 是一款强大的.NET开源后台任务利器,无需Windows服务/任务计划程序. 可以使用于ASP.NET 应用也 ...

  9. Java开发程序,使用编辑器编写创建Java项目、类

    打开Eclipse 出现界面 工作空间的路径可以选择一个大空间的磁盘存放,点击确定: 创建:程序左上角-文件-新建-JAVA项目 输入项目名(项目名不能为中文),点击完成: 在   包资源管理器中点击 ...

  10. 关于在云服务器上部署tomcat笔记

    tomcat无法启动的原因有:1.由于在tomcat里JVM没有配好,他默认的是批向c:\program file\Java下的那一个,如果你确认 这没有删掉,JVM指向你自己安装的jdk\jre试试 ...