zoukankan      html  css  js  c++  java
  • 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",@"1",@"two",@"2", nil];
        arr=[NSMutableArray arrayWithObjects:@"少年",@"青年",@"老年", nil];
        number=1;
        label=[[UILabel alloc]initWithFrame:CGRectMake(50, 100, 100,100)];
        label.font=[UIFont systemFontOfSize:12];
        label.textAlignment=NSTextAlignmentCenter;
        label.text=@"test";
        label.tag=1;
        label.backgroundColor=[UIColor cyanColor];
        
        age=[[UILabel alloc]initWithFrame:CGRectMake(200, 100, 100,100)];
        age.font=[UIFont systemFontOfSize:12];
        age.textAlignment=NSTextAlignmentCenter;
        age.text=@"25";
        age.tag=2;
        age.backgroundColor=[UIColor cyanColor];
        
        [self insertCoreData];
        [self dataFetchRequest];
        UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame=CGRectMake(50, 230, 100, 100);
        [btn setTitle:@"删除" forState:UIControlStateNormal];
        btn.backgroundColor=[UIColor purpleColor];
        [btn addTarget:self action:@selector(delete) forControlEvents:UIControlEventTouchUpInside];
        
        UIButton *btn1=[UIButton buttonWithType:UIButtonTypeCustom];
        btn1.frame=CGRectMake(50, 340, 100, 100);
        [btn1 setTitle:@"查找" forState:UIControlStateNormal];
        btn1.backgroundColor=[UIColor purpleColor];
        [btn1 addTarget:self action:@selector(seek) forControlEvents:UIControlEventTouchUpInside];
        UIButton *btn2=[UIButton buttonWithType:UIButtonTypeCustom];
        btn2.frame=CGRectMake(50, 450, 100, 100);
        [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:@"2"];
            //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:2];
            
            
            NSLog(@"***************age.text0=%@",jsonObjectArr[0]);
            NSLog(@"***************age.text1=%@",jsonObjectArr[1]);
            NSLog(@"***************age.text3=%@",jsonObjectArr[2]);
    
        }
    }
    -(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
  • 相关阅读:
    认识ZooKeeper
    html5实现本页面元素拖放和本地文件拖放
    查询算法(一) 顺序查询与折半查询
    Shell排序算法和合并排序算法
    堆排序及修改选择排序
    Java实现冒泡排序,选择排序,插入排序
    穷举算法和递推算法(Java)
    由Spring框架中的单例模式想到的
    Java 线程面试题 Top 50(转 ImportNew)
    启动idea项目Debug模式时,报错Command line is too long
  • 原文地址:https://www.cnblogs.com/sayimba/p/5663596.html
Copyright © 2011-2022 走看看