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的图像风格转换.既然感兴趣就直接开始干,读了论文,一知半解:看了别人的源码,才算大概了解的具体的实现,也惊叹别 ...
随机推荐
- .net学习笔记----会话状态Session
一.会话状态Session Session用于服务器端状态管理,使用Session之后,每个客户端都可以将实际的数据保存在服务器上,对于每个客户端的数据,将会生成一个对应的唯一的key(保存在客户端) ...
- 蓝桥杯 入门训练 Fibonacci数列(水题,斐波那契数列)
入门训练 Fibonacci数列 时间限制:1.0s 内存限制:256.0MB 问题描述 Fibonacci数列的递推公式为:Fn=Fn-1+Fn-2,其中F1=F2=1. 当n比较大时,Fn也非 ...
- Windows 10 上强制Visual Studio以管理员身份运行
Windows 10 的一个既安全又蛋疼之处是UAC的行为被改变了.以往在Windows 7中,只要关闭了UAC,自己的帐号又是本机管理员组的,任何程序都会以管理员身份启动.然而,在Windows 8 ...
- Java线程面试题 Top 50
转自:http://www.importnew.com/12773.html 不管你是新程序员还是老手,你一定在面试中遇到过有关线程的问题.Java语言一个重要的特点就是内置了对并发的支持,让Java ...
- 开发Android 范的错误
1 在onCreate(Bundle savedInstanceState)方法中, 按钮单击事件的实现直接写在onCreate方法了里,这样就好导致这个按钮只能触发一次, 因为在Android体系中 ...
- dip,px,pt,sp的区别
dip: device independent pixels(设备独立像素). 不同设备有不同的显示效果,这个和设备硬件有关,一般我们为了支持WVGA.HVGA和QVGA 推荐使用这个,不依赖像素. ...
- Hark的数据结构与算法练习之地精(侏儒)排序
算法说明 地精排序是交换排序的一种,它是冒泡排序的一种改良,我感觉和鸡尾酒排序挺像的. 不同之处是鸡尾酒排序是从小到大,然后再从大到小切换着排序的.而地精排序是上来先从小到大排序,碰到交换到再从大到小 ...
- cf 333b
G - Chips Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Submit S ...
- nginx查看post请求日志
在http段加上 log_format access '$remote_addr - $remote_user [$time_local] "$request" $status $ ...
- 使用“Empty 模式”改进 Null Object
概述 Null Object 是Martin 大师提出的一种重构手段,其思想就是通过多态(派生一个Null对象)来减少逻辑(if … then …else)的判断. 而.NET中已经有Null Obj ...