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的图像风格转换.既然感兴趣就直接开始干,读了论文,一知半解:看了别人的源码,才算大概了解的具体的实现,也惊叹别 ...
随机推荐
- windows下同时安装python2与python3
由于python2与python3并不相互兼容,并且差别较大,所以有时需要同时安装,但在操作命令行时,怎么区别python2与python3呢? 1.下载并安装Python 2.7.9和Python ...
- javascript集合的交,并,补,子集的操作实现
可能新的ECMA规范里已有了这些的实现, 但能自己从头开始实现,感觉也非常不错的哟... function Set() { var items = {}; this.has = function(va ...
- C++ 学习的网站
C++入门教程: http://see.xidian.edu.cn/cpp/biancheng/cpp/rumen/
- VMware-WorkStation 序列号(转)
原文摘自: http://blog.csdn.net/twlkyao/article/details/8681927 最近升级了下虚拟机,可是还要序列号,网上找了几个可以用的,留着,备用. 9.0.2 ...
- SURF算法与源码分析、下
上一篇文章 SURF算法与源码分析.上 中主要分析的是SURF特征点定位的算法原理与相关OpenCV中的源码分析,这篇文章接着上篇文章对已经定位到的SURF特征点进行特征描述.这一步至关重要,这是SU ...
- 详细剖析电脑hosts文件的作用和修改
提到电脑系统中的hosts文件,如果不是太熟悉的话,还真是闻所未闻,一是由于系统的hosts文件为系统属性,在系统默认设置下,我们根本无法看到它的存在,而是由于身处系统深层文件夹内,我们一般也无法察觉 ...
- java实例--海盗的最优方案
package unit4; public class Pirate{ private String name; private int[] schemes; private int index; p ...
- Android Studio导入Fresco
大概一周之前,Facebook开源了专为Android系统定制的图片下载缓存工具,当天该消息就上了各大技术论坛网站的头条,也成为了各个技术群里讨论的最主要的话题.也就在当天stay4it的QQ群里面就 ...
- 理解Null,Undefined,NAN
1.null表示尚未存在的对象,转为数值时为0.它表示"没有对象",即该处不应该有值,常用来表示函数企图返回一个不存在的对象.null是一种特殊的object(引用类型),代表一个 ...
- PDA移动POS开单扫描打票收银系统-带来零售批发 新的技术 新的手段!!
手持POS终端高清彩屏,清晰.美观.大方,适用于仓库.超市.服装.食品.批发零售.手机电脑等企业管理.可与管理软件灵活对接.1:员工记不住价格,产品名称,只要有PDA扫描,价格,库存,直接开销售单,打 ...