coreDate 简化版
建表:

自动生成:

代码:
//
// RootViewController.m
// coreDate 简化版
#import "RootViewController.h"
#import "entity.h"
#import "AppDelegate.h"
@interface RootViewController ()
{
UILabel *label;
NSInteger number;
NSDictionary *Dic;
NSMutableArray *arr;
UILabel *age;
BOOL isFirst;
}
@end @implementation RootViewController - (void)viewDidLoad {
[super viewDidLoad];
isFirst=NO; Dic=[[NSDictionary alloc]initWithObjectsAndKeys:@"one",@"",@"two",@"", nil];
arr=[NSMutableArray arrayWithObjects:@"少年",@"青年",@"老年", nil];
number=;
label=[[UILabel alloc]initWithFrame:CGRectMake(, , ,)];
label.font=[UIFont systemFontOfSize:];
label.textAlignment=NSTextAlignmentCenter;
label.text=@"test";
label.tag=;
label.backgroundColor=[UIColor cyanColor]; age=[[UILabel alloc]initWithFrame:CGRectMake(, , ,)];
age.font=[UIFont systemFontOfSize:];
age.textAlignment=NSTextAlignmentCenter;
age.text=@"";
age.tag=;
age.backgroundColor=[UIColor cyanColor]; [self insertCoreData];
[self dataFetchRequest];
UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
btn.frame=CGRectMake(, , , );
[btn setTitle:@"删除" forState:UIControlStateNormal];
btn.backgroundColor=[UIColor purpleColor];
[btn addTarget:self action:@selector(delete) forControlEvents:UIControlEventTouchUpInside]; UIButton *btn1=[UIButton buttonWithType:UIButtonTypeCustom];
btn1.frame=CGRectMake(, , , );
[btn1 setTitle:@"查找" forState:UIControlStateNormal];
btn1.backgroundColor=[UIColor purpleColor];
[btn1 addTarget:self action:@selector(seek) forControlEvents:UIControlEventTouchUpInside];
UIButton *btn2=[UIButton buttonWithType:UIButtonTypeCustom];
btn2.frame=CGRectMake(, , , );
[btn2 setTitle:@"增加" forState:UIControlStateNormal];
btn2.backgroundColor=[UIColor purpleColor];
[btn2 addTarget:self action:@selector(insertCoreData) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn1];
[self.view addSubview:btn];
[self.view addSubview:btn2];
[self.view addSubview:label];
[self.view addSubview:age];
} -(void)seek
{
UIApplication * application = [UIApplication sharedApplication];
//默认appDelegate就是UIApplication的代理
AppDelegate * delegate = application.delegate;
NSManagedObjectContext *context = [delegate managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *ent = [NSEntityDescription entityForName:@"Entity" inManagedObjectContext:context];
[fetchRequest setEntity:ent];
NSError *error;
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error]; if (![fetchedObjects count]) {
NSLog(@"fetchedObjects 已全部删除");
} for (entity *info in fetchedObjects) { NSLog(@"count:%ld",(unsigned long)[fetchedObjects count]); //adress字典查找
NSString *jsonStringDic=[[NSString alloc]initWithFormat:@"%@",info.address];
//字符串转化成data类型
NSData *dataDic=[jsonStringDic dataUsingEncoding:NSUTF8StringEncoding];
NSError *errDic; //将data类型转化成字典
id jsonObjectDic = [NSJSONSerialization JSONObjectWithData:dataDic
options:NSJSONReadingAllowFragments
error:&errDic]; //根据key进行查找
label.text=[jsonObjectDic objectForKey:@""];
//age 数组查找
NSString *jsonStringArr=[[NSString alloc]initWithFormat:@"%@",info.age];
NSData *dataArr=[jsonStringArr dataUsingEncoding:NSUTF8StringEncoding];
NSError *errArr;
id jsonObjectArr=[NSJSONSerialization JSONObjectWithData:dataArr options:NSJSONReadingAllowFragments error:&errArr];
age.text=[jsonObjectArr objectAtIndex:]; NSLog(@"***************age.text0=%@",jsonObjectArr[]);
NSLog(@"***************age.text1=%@",jsonObjectArr[]);
NSLog(@"***************age.text3=%@",jsonObjectArr[]); }
}
-(void)delete
{ UIApplication * application = [UIApplication sharedApplication];
//默认appDelegate就是UIApplication的代理
AppDelegate * delegate = application.delegate;
NSManagedObjectContext *context = [delegate managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *ent = [NSEntityDescription entityForName:@"Entity" inManagedObjectContext:context];
[fetchRequest setEntity:ent]; NSError *error;
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error]; for (entity *info in fetchedObjects) {
NSLog(@"%@",info);
//删除 整体都会删除
[context deleteObject:info];
}
number ++;
//coredata 数据保存
if ([context save:&error]) {
//更新成功
NSLog(@"更新成功");
}
} -(void)insertCoreData
{
if (isFirst) {
UIApplication * application = [UIApplication sharedApplication];
//默认appDelegate就是UIApplication的代理
AppDelegate * delegate = application.delegate;
NSManagedObjectContext *context = [delegate managedObjectContext]; entity *entity = [NSEntityDescription insertNewObjectForEntityForName:@"Entity" inManagedObjectContext:context]; entity.name = @"test"; NSError *errDic;
//字典转化成data类型,再转化成string进行保存
//注意:是 NSData 不是 NSDate(时间)
NSData *dataDic=[NSJSONSerialization dataWithJSONObject:Dic options:NSJSONWritingPrettyPrinted error:&errDic];
NSString *jsonString=[[NSString alloc]initWithData:dataDic encoding:NSUTF8StringEncoding]; entity.address=jsonString; //保存数组
NSError *errArr;
NSData *dataArr=[NSJSONSerialization dataWithJSONObject:arr options:NSJSONWritingPrettyPrinted error:&errArr];
NSString *jsonArr=[[NSString alloc]initWithData:dataArr encoding:NSUTF8StringEncoding]; entity.age=jsonArr; NSError *error;
if(![context save:&error])
{
NSLog(@"不能保存:%@",[error localizedDescription]);
}
}
else
{
isFirst=YES;
}
}@end
coreDate 简化版的更多相关文章
- 机器学习——支持向量机(SVM)之拉格朗日乘子法,KKT条件以及简化版SMO算法分析
SVM有很多实现,现在只关注其中最流行的一种实现,即序列最小优化(Sequential Minimal Optimization,SMO)算法,然后介绍如何使用一种核函数(kernel)的方式将SVM ...
- 学习OpenCV——ORB简化版&Location加速版
根据前面surf简化版的结构,重新把ORB检测的代码给简化以下,发现虽然速度一样,确实能省好多行代码,关键是有 BruteForceMatcher<HammingLUT>matcher的帮 ...
- [vijos P1014] 旅行商简化版
昨天早上上课讲旅行商问题,有点难,这周抽空把3^n的算法码码看.不过这个简化版已经够折腾人了. 其一不看解析不知道这是双进程动态规划,不过我看的解析停留在f[i,j]表示第一个人走到i.第二个人走到j ...
- CoreDate的使用
勾选 xcode的 CoreDate会帮我们自动创建 CoreData 但是我们通常不那样使用,通常把 CoreDate 在单利类中创建, // // ZYDAO.h // StoryboardTes ...
- 简化版的Flappy Bird开发过程(不使用第三方框架)
目录 .1构造世界 .2在世界中添加元素 .3碰撞检测 .4添加动画特效 .5总结 .0 开始之前 之前曾经用Html5/JavaScript/CSS实现过2048,用Cocos2d-html5/Ch ...
- vijosP1014 旅行商简化版
vijosP1014 旅行商简化版 链接:https://vijos.org/p/1014 [思路] 双线DP. 设ab,ab同时走.用d[i][j]表示ab所处结点i.j,且定义i>j,则有转 ...
- redux-simple 简化版的redux
作为react的粉丝,当然要吐槽一下react组件通信问题.react的单向数据流是组件通信的一大阻碍,只允许父组件向子组件传值,子组件向父组件传值只能通过父组件向子组件传递回调函数实现.如果在深层次 ...
- 模拟实现简化版List迭代器&嵌入List
1.迭代器(iterators)概念(1)迭代器是一种抽象的设计概念,其定义为:提供一种方法,使他能够按顺序遍历某个聚合体(容器)所包含的所有元素,但又不需要暴露该容器的内部表现方式. (2)迭代器是 ...
- A Neural Algorithm of Artistic Style 图像风格转换 - keras简化版实现
前言 深度学习是最近比较热的词语.说到深度学习的应用,第一个想到的就是Prisma App的图像风格转换.既然感兴趣就直接开始干,读了论文,一知半解:看了别人的源码,才算大概了解的具体的实现,也惊叹别 ...
随机推荐
- 何时使用hadoop fs、hadoop dfs与hdfs dfs命令(转)
hadoop fs:使用面最广,可以操作任何文件系统. hadoop dfs与hdfs dfs:只能操作HDFS文件系统相关(包括与Local FS间的操作),前者已经Deprecated,一般使用后 ...
- UML状态图和活动图
UML状态图和活动图 统一建模语言UML(Unified Modeling Language)是非专利的第三代建模和规约语言.UML是一种开放的方法,用于说明.可视化.构建和编写一个正在开发的.面向对 ...
- 配置redis外网可访问
redis采用的安全策略,默认会只准许本地访问 通过简单配置,完成允许外网访问 [root@cache01 conf]# egrep "(^bind|#bind|# bind)" ...
- [原]ASP.NET 数据库访问通用工具
在工作中,有很多项目已上线后,很多项目的数据库服务器都不会对外开放的,外网想直接访问客户数据库服务器时,可能会出现困难. 这时就需要一个可以查询,更新数据库操作的页面了: 本来用sql语句直接操作数据 ...
- 建模算法(九)——拟合
一.线性最小二乘法 1.基本思路 令,其r(x)是事先选定的一组线性无关的函数.ak是待定系数.然后拟合的准则就是使得yi与f(xi)的距离的平方和最小,称之为最小二乘准则 2.系数的确定 ,要使距离 ...
- 建模算法(十)——灰色理论之关联度分析
一.数据变换技术 为了保证建模的质量和系统分析结果的准确性,对原始的数据要进行去量纲处理. 1.定义 设有序列,则成映射为序列x到序列y的数据变换. (1) f 是初值化变换. (2) f 是均值化变 ...
- Kinect学习笔记(五)——更专业的深度图
这一节的内容就是把深度图转换为彩色图,然后不再使用硬解码,而是继续采用sdk里面封装好的功能来减少测量的误差,以及避免转换为灰度图时,出现绿巨人时候的掉针的bug. 下面直接 ...
- centOS下安装node4.x
在以前 Node 0.x 的时候,只需要通过 yum install -y nodejs 就可以把 Node 安装好了.但是 yum 安装的 Node 只能安装到 0.10.40 版的,并不能安装到 ...
- Kali Linux 2016.2发布提供虚拟机以及系统镜像下载
Kali Linux 2016.2发布提供虚拟机以及系统镜像下载 Kali Linux 2016.2发布提供虚拟机以及系统镜像下载,本次Kali Linux 2016.2提供了五种桌面模式,分别为 ...
- Oracle 使用小计
1.Sequence 1.1 什么是Sequence? Sequence是oracle提供的一个对象,用于产生自增的主键.这与sql server的identity是类似的. 从数学的角度来说,其为一 ...