保存和加载数据 - CoreData

Save and Load Data - CoreData(保存和加载数据 - CoreData)

本文介绍了保存和加载数据 - CoreData的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 CoreData 的新手,在我的 iPhone 应用程序中,我想知道如何保存一些文本,然后将其重新加载.但诀窍是,当 UIDatePicker 中的日期与我的日期相同时重新加载它像日历一样保存它.

I am new to CoreData and in my iPhone app I want to know how to save some text, then load it back in. But the trick is, loading it back in when the date in the UIDatePicker is the same as when I saved it, like a calendar.

感谢您的回复,尤其是代码示例.但是,我有一些问题了解这是如何组合在一起的,请原谅我.我想了解它是如何工作的,但同时让它发挥作用真的很有帮助.

Thanks for the replies, especially the for the code sample. However I have some issues understand how this fits together, forgive me. I want to learn how this works, but to get it working in the mean time will really be helpful.

大概我把你的第一个代码块放在了完成编辑方法中(离开文本框时保存),然后把第二个块放在日期更改方法中(更改日期时加载)?

Presumably I put your first code chunk in the did finish editing method (save when leaving text box) and then the second chunk in the date changed method (load when changing the date)?

这段代码,是否使用实体和属性名称?我究竟需要如何设置我的 xcdatamodel 文件?我需要使用此代码还是仅演示我需要哪些实体和属性?

This code, does this use the entity and property names? How exactly do I need to setup my xcdatamodel file? Do I need to use this code or does this just demonstrate what entities and properties I need?

DatedText{
    savedText:String
    dateSaved:Date
}

这一行说mo"未声明:(已修复)

This line says 'mo' is undeclared: (FIXED)

    newDatedText=mo=[NSEntityDescription insertNewObjectForEntityForName:@"DateText"
inManagedObjectContext:theManagedObjectContext];

大概在someText"下面是我要保存的文本?而aDateObject"是我的 UIDatePicker 中的日期?

Presumably below 'someText' is the text I want save? And the 'aDateObject' is the date in my UIDatePicker?

[newDatedText setValue:someText forKey:@"savedText"];
    [newDatedText setValue:aDateObject forKey:@"dateSaved"];

在非结构或联合的情况下请求成员moc":

Request for member 'moc' in something not a structure or union:

NSEntityDescription *testEntity=[NSEntityDescription entityForName:@"DatedText" inManagedObjectContext:self.moc];

什么是targetDate"(未声明)?UIDatePicker 再次约会?

Whats 'targetDate' (undeclared)? UIDatePicker date again?

NSPredicate *pred=[NSPredicate predicateWithFormat:@"dateSaved==%@", targetDate];

在非结构或联合的情况下请求成员moc":

Request for member 'moc' in something that's not a structure or a union:

NSArray *fetchedObjs=[self.moc executeFetchRequest:theFetch error:&fetchError];

推荐答案

首先,您需要一个数据模型来反映您想要保存的内容,在这种情况下,需要一些文本和一个日期.所以:

To start you need a data model to reflect what you want to save, in this case, some text and a date. So:

DatedText{
    savedText:String
    dateSaved:Date
}

要创建一个新的 DatedText 对象(没有自定义类):

To create a new DatedText object (without a custom class) do:

NSManagedObject *newDatedText;
newDatedText=[NSEntityDescription insertNewObjectForEntityForName:@"DateText" inManagedObjectContext:theManagedObjectContext];
[newDatedText setValue:someText forKey:@"savedText"];
[newDatedText setValue:aDateObject forKey:@"dateSaved"];

NSError *saveError=nil;
[theManagedObjectContext save:&saveError];
if (saveError!=nil) {
    NSLog(@"[%@ saveContext] Error saving context: Error=%@,details=%@",[self class], saveError,saveError.userInfo);
}

要检索具有特定日期的 DatedText 对象,您可以像这样创建提取:

To retrieve a DatedText object with a specific date you create a fetch like so:

NSFetchRequest *fetch=[[NSFetchRequest alloc] init];
NSEntityDescription *testEntity=[NSEntityDescription entityForName:@"DatedText" inManagedObjectContext:self.moc];
[fetch setEntity:testEntity]; 
NSPredicate *pred=[NSPredicate predicateWithFormat:@"dateSaved==%@", targetDate];
[fetch setPredicate:[NSArray arrayWithObject:pred]];

NSError *fetchError=nil;
NSArray *fetchedObjs=[theManagedObjectContext executeFetchRequest:theFetch error:&fetchError];
if (fetchError!=nil) {
    NSLog(@" fetchError=%@,details=%@",fetchError,fetchError.userInfo);
    return nil;
}

fetchedObjs 现在将包含所有 DatedText 对象的数组,这些对象具有与 targetDate 相同的 savedDate.

fetchedObjs will now contain an array of all DatedText objects with the same savedDate as targetDate.

这篇关于保存和加载数据 - CoreData的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:保存和加载数据 - CoreData

基础教程推荐