coreDate 简化版

建表:

coreDate 简化版

自动生成:

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
上一篇:【转】NotificationCopat.Builder全部设置


下一篇:[ActionScript 3.0] AS3.0 获取像素点的灰度