博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iphone开发中的数据存储:Core Data
阅读量:7120 次
发布时间:2019-06-28

本文共 3579 字,大约阅读时间需要 11 分钟。

同样是之前的存储四个textField的例子

在项目中创建后缀为” .xcdatamodel “的存储文件,在其attribute中创建属性,lineNum和lineText,类型为integer116和String

 

然后直接上代码:

- (void)applicationWillResignActive:(NSNotification *)notification {    //获取appDelegate    BIDAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];    //获取上下文context    NSManagedObjectContext *context = [appDelegate managedObjectContext];        NSError *error;        for (int i = 1; i <= 4; i++) {        NSString *fieldName = [NSString stringWithFormat:@"line%d", i];        UITextField *theField = [self valueForKey:fieldName];        //创建提取请求        NSFetchRequest *request = [[NSFetchRequest alloc] init];        //创建托管对象(Managed Objects)        NSEntityDescription *entityDescription = [NSEntityDescription                                                  entityForName:@"Line"                                                  inManagedObjectContext:context];        //为请求设置Entity,指定希望的对象实体        [request setEntity:entityDescription];                //谓词(predicate)(断言?)类似于SQL中的where语言        //这句的意思是通知提取请求仅搜索lineNum 属性设置为i 的对象        NSPredicate *pred = [NSPredicate                             predicateWithFormat:@"(lineNum = %d)", i];        //设置断言        [request setPredicate:pred];                NSManagedObject *theLine = nil;                //执行提取请求,执行之后,context将跟踪你对该数组(objects)中返回的托管对象(theLine)的任何修改,最终发送sava:时保存        NSArray *objects = [context executeFetchRequest:request                                                  error:&error];                if (objects == nil) {            NSLog(@"There was an error!");            // Do whatever error handling is appropriate        }        if ([objects count] > 0)//原本存在,因为有设置断言,所以这里的count=1            theLine = [objects objectAtIndex:0];        else//初次存储            theLine = [NSEntityDescription                       insertNewObjectForEntityForName:@"Line"                       inManagedObjectContext:context];                [theLine setValue:[NSNumber numberWithInt:i] forKey:@"lineNum"];        [theLine setValue:theField.text forKey:@"lineText"];            }    //保存数据    [context save:&error];}

 

 

viewDidLoad中

- (void)viewDidLoad {
[super viewDidLoad]; // Do any additional setup after loading the view from its nib. BIDAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; NSManagedObjectContext *context = [appDelegate managedObjectContext]; NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Line" inManagedObjectContext:context]; NSFetchRequest *request = [[NSFetchRequest alloc] init]; [request setEntity:entityDescription]; NSError *error; NSArray *objects = [context executeFetchRequest:request error:&error]; if (objects == nil) {
NSLog(@"There was an error!"); // Do whatever error handling is appropriate } for (NSManagedObject *oneObject in objects) {
NSNumber *lineNum = [oneObject valueForKey:@"lineNum"]; NSString *lineText = [oneObject valueForKey:@"lineText"]; NSString *fieldName = [NSString stringWithFormat:@"line%d", [lineNum integerValue]]; UITextField *theField = [self valueForKey:fieldName]; theField.text = lineText; } UIApplication *app = [UIApplication sharedApplication]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillResignActive:) name:UIApplicationWillResignActiveNotification object:app]; }

转载地址:http://ztiel.baihongyu.com/

你可能感兴趣的文章
最佳在线图表软件
查看>>
手挽手带你学React:三档 React-router4.x的使用
查看>>
React Hooks 梳理
查看>>
垃圾回收之引用计数
查看>>
Java的Interrupt与线程中断
查看>>
前端实用小工具
查看>>
大型云原生项目在数字化企业落地过程解密
查看>>
CentOS 7 编译安装 PHP 7
查看>>
vue跳转传参刷新后参数消失
查看>>
Java™ 教程(原子变量)
查看>>
非对称加密算法--RSA加密原理及运用
查看>>
精读《如何编译前端项目与组件》
查看>>
[ 逻辑锻炼] 用 JavaScript 做一个小游戏 ——2048 (详解版)
查看>>
【产品】产品经理常用的五大分析法
查看>>
Javascript二进制运算符的一些运用场景
查看>>
常见Promise面试题
查看>>
一个专科生学习JAVA目标月薪2万是否不切实际?
查看>>
保持ssh的连接不断开
查看>>
Java 高级算法——数组中查询重复的数字之二
查看>>
897-递增顺序查找树
查看>>