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的图像风格转换.既然感兴趣就直接开始干,读了论文,一知半解:看了别人的源码,才算大概了解的具体的实现,也惊叹别 ...
随机推荐
- 【Ubuntu14.04.1】设置开机可以Root用户身份登录
$ sudo gedit /usr/share/lightdm/lightdm.conf.d/50-ubuntu.conf [SeatDefaults]user-session=ubuntugreet ...
- Android之UI控件
本文主要包括以下内容 Spinner的使用 Gallery的使用 Spinner的使用 Spinner的实现过程是 1. 在xml文件中定义Spinner的控件 2. 在activity中获取Spin ...
- Java中比较不同的MD5计算方式
在项目中经常需要使用计算文件的md5,用作一些用途,md5计算算法,通常在网络上查询时,一般给的算法是读取整个文件的字节流,然后计算文件的md5,这种方式当文件较大,且有很大并发量时,则可能导致内存打 ...
- JAVA反射机制(转)
JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法: 对于任意一个对象,都能够调用它的任意一个方法和属性: 这种动态获取的信息以及动态调用对象的方法的功能称为java语言 ...
- android 检测sqlite数据表中字段(列)是否存在 (转)
原文摘自 http://www.tuicool.com/articles/jmmMnu 一般数据库升级时,需要检测表中是否已存在相应字段(列),因为列名重复会报错.方法有很多,下面列举2种常见的方式: ...
- isnull的使用方法
is null 查看列数据为空 select*from lrb where lrid is null ISNULL使用指定的替换值替换 NULL. 语法ISNULL ( check_express ...
- 【HTML5】Server-Sent服务器发送事件
Server-Sent 事件 - 单向消息传递 Server-Sent 事件指的是网页自动获取来自服务器的更新. 以前也可能做到这一点,前提是网页不得不询问是否有可用的更新.通过服务器发送事件,更新能 ...
- js:语言精髓笔记11--动态语言特性(1)
语言:程序最终被表达为数据(结构)和逻辑(算法),命令式和说明式/函数式语言分别从这两方面分类: 动态:在语言陈述时无法确定,必须在计算机执行时才能确定语言关系:JS是完全动态语言,导致其不确定性一般 ...
- c#知识总结2
四.C#类型转换 类型转换就是把一种类型转换成为另一种类型. 隐式类型转换:c#默认的以安全方式进行的转换.例如小整数类型转换为大整数类型.派生类转换为基类 显式类型转换:用户使用的预定义的函数显式完 ...
- 2016 Multi-University Training Contest 8
solved 4/11 2016 Multi-University Training Contest 8 贪心 1001 Ball(BH) 代码: #include <bits/stdc++.h ...