iphone开发中的数据存储:Core Data

简介:

同样是之前的存储四个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];
}
复制代码

本文转自老Zhan博客园博客,原文链接:http://www.cnblogs.com/mybkn/archive/2012/03/31/2427157.html,如需转载请自行联系原作者

相关文章
|
编解码 iOS开发 UED
响应式设计在 iPhone 开发适配中的具体应用
【10月更文挑战第23天】响应式设计在 iPhone 开发适配中扮演着至关重要的角色,它能够帮助我们打造出适应不同屏幕尺寸和用户需求的高质量应用。通过合理运用响应式设计的原则和方法,我们可以在提供良好用户体验的同时,提高开发效率和应用的可维护性。
|
编解码 测试技术 iOS开发
iPhone 屏幕尺寸和开发适配
【10月更文挑战第23天】iPhone 的屏幕尺寸变化给开发者带来了一定的挑战,但也为创新提供了机遇。通过深入了解不同屏幕尺寸的特点,遵循适配原则和策略,运用合适的技巧和方法,我们能够为用户提供在不同 iPhone 机型上都具有良好体验的应用。在未来,随着技术的不断进步,我们还需要持续学习和适应,以满足用户对优质应用体验的不断追求。
|
数据采集 iOS开发 Python
Chatgpt教你开发iPhone风格计算器,Python代码实现
Chatgpt教你开发iPhone风格计算器,Python代码实现
285 1
|
编解码 iOS开发
iphone 开发的基本入门知识
iphone 开发的基本入门知识
420 0
|
Shell iOS开发
iOS逆向:tweak开发教程(iPhone/tool)
iOS逆向:tweak开发教程(iPhone/tool)
1641 0
iOS逆向:tweak开发教程(iPhone/tool)
「镁客早报」iPhone或将在今年采用三摄;传Facebook致力于开发语音助力服务与亚马逊、苹果竞争
亚马逊向美国Alexa设备推免费音乐服务;视频会议软件开发商Zoom纳斯达克上市。
379 0
|
前端开发
「CSS畅想」技术传递乐趣,好友说她想要七龙珠,我帮她召唤了神龙
前端技术从业者与非技术好友互动,好友说她想要七龙珠,我帮她召唤了神龙。
267 1
|
6天前
|
人工智能 数据可视化 安全
王炸组合!阿里云 OpenClaw X 飞书 CLI,开启 Agent 基建狂潮!(附带免费使用6个月服务器)
本文详解如何用阿里云Lighthouse一键部署OpenClaw,结合飞书CLI等工具,让AI真正“动手”——自动群发、生成科研日报、整理知识库。核心理念:未来软件应为AI而生,CLI即AI的“手脚”,实现高效、安全、可控的智能自动化。
18011 12
王炸组合!阿里云 OpenClaw X 飞书 CLI,开启 Agent 基建狂潮!(附带免费使用6个月服务器)
|
17天前
|
人工智能 JSON 机器人
让龙虾成为你的“公众号分身” | 阿里云服务器玩Openclaw
本文带你零成本玩转OpenClaw:学生认证白嫖6个月阿里云服务器,手把手配置飞书机器人、接入免费/高性价比AI模型(NVIDIA/通义),并打造微信公众号“全自动分身”——实时抓热榜、AI选题拆解、一键发布草稿,5分钟完成热点→文章全流程!
29546 141
让龙虾成为你的“公众号分身” | 阿里云服务器玩Openclaw