保存并加载数据 - CoreData [英] Save and Load Data - CoreData

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

问题描述

我对CoreData非常新,在我的iPhone应用程序中,我想知道如何保存一些文本,然后加载它。但诀窍是,当UIDatePicker中的日期是相同的时候加载它。我保存了它,就像一个日历。

I am very 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.

希望你能帮助。它非常重要我学会如何做这个很快,所以任何帮助大量赞赏,感谢。

Hope you can help in anyway. Its pretty important I learn how to do this fairly soon, so any help is massively appreciated, thanks.

更新:
感谢您的回复,特别是代码示例。但是我有一些问题,理解这是如何组合在一起,请原谅我。我想了解这是如何工作的,但是让它工作在同一时间将真正有用的。

UPDATE: 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.

我理解了一些很多错误,只是固定在正确的方式是我的问题。希望你可以帮助。

I understand some many of these errors, only fixing them in the correct manner is my issue. Hope you can help.

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

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
}

undeclared:(FIXED)

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];

Whats'targetDate'(undeclared)? UIDatePicker日期?

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

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

在不是结构或联合的成员moc请求:

Request for member 'moc' in something thats 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 对象)do:

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 具有特定日期的对象创建一个fetch,如下所示:

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 包含与 savedDate targetDate DatedText c>。

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆