核心数据编辑属性 [英] Core Data Edit Attributes

查看:75
本文介绍了核心数据编辑属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我对核心数据确实很陌生,但是我通过了一个教程并几乎了解了它,至少在大多数情况下都知道了。但是我仍然有1个我在任何地方都找不到的问题。看起来真的很简单,但是确实如此。如果我在一个实体中有两个字符串,可以说:

So im really new to core data, but i went through a tutorial and pretty much understand it, well at least the idea behind most of the things. But I still have 1 question that i cant find anywhere. It seems really simple but here it is. If I were to have two strings inside one entity lets say:

1.name

2.position

1.name
2.position

如果已经输入了名称,我如何允许用户在textField中输入文本,并在以后将其分配给他们的位置?即使有20个名字,也考虑不重复吗?

If the name is already entered how might i allow a user to enter text into a textField and assign it to their position at a later time? Even if there were 20 names, considering no duplicates?

我以为可能是这样的事情……但它似乎不起作用。

I was thinking it might be something like this...But it doesnt seem to work.

UserInfo *userInfo = (UserNumber *)[NSEntityDescription insertNewObjectForEntityForName:@"UserInfo" inManagedObjectContext:managedObjectContext];

if ([userName isEqualToString:"@James"]) {
    userInfo.Position = nameField.text;
}


推荐答案

在上面的代码中,将(UserNumber *)强制转换为要声明为(UserInfo *)的对象?这是什么,您为什么要这样做?

On the code above you are casting (UserNumber*) to an object that you are declaring as (UserInfo*)? Which is what and is there any reason why you are doing that?

如果我正确理解了您的问题,则希望创建一个仅预填充用户名的记录,然后允许该记录在以后的阶段进行更新。

If I understand your question correctly, you want to create a record with only the username pre-populated and then allow that record to be updated at a later stage.

我假设您的实体名为 UserInfo ,并且为它创建了2个NSString属性- userName position 。我还假设您已经为UserInfo创建了类文件,并将标头导入了相关的视图控制器。

I will assume your entity is called UserInfo and that there are 2 NSString properties created for it - userName and position. I also assume you have created the class files for UserInfo and imported the header into the relevant view controllers.

这里是您的操作方式:

1)首先,假设您在 UITextField * userNameField 中键入了用户名,让我们创建一个新记录。

1) Firstly, assuming you have username typed in a UITextField *userNameField, let's create a new record.

UserInfo *userInfo = (UserInfo*)[NSEntityDescription insertNewObjectForEntityForName:@"UserInfo" inManagedObjectContext:self.managedObjectContext];
[userInfo setValue:userNameField.text forKey:@"userName"];

这将创建 UserInfo 的新实例在您的托管对象上下文中,将 userName 的值设置为 userNameField.text

This will create a new instance of UserInfo in your managed object context and set the value of userName to the value on userNameField.text

然后,在稍后的阶段,用户将到达一个可以更新其应用程序中记录的位置(您可能需要在此处的某个地方考虑​​身份验证)。您将获取与您指定的用户名匹配的记录:

Then at a later stage a user will get to a point where they can update their records in your app (you may need to think about authentication somewhere here). You will fetch the record that matches your specified username:

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSPredicate *userNamePredicate = [NSPredicate predicateWithFormat:@"(userName == %@)", userNameField.text];
[fetchRequest setPredicate:userNamePredicate];
NSEntityDescription *userInfo = [NSEntityDescription entityForName:@"UserInfo" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:userInfo];
NSError *error;
NSArray *fetchRequestArray = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
[fetchRequest release];

如果fetchRequest与您的 userNameField.text匹配参数,它们将保存在 fetchRequestArray 中。如果采取必要的步骤使 userName 属性为强制性和唯一性,则最多只能有一个对象。

If the fetchRequest found match(es) to your userNameField.text paramater, they will be saved in the fetchRequestArray. There should only be a maximum of one object there if you take the necessary steps to make the userName property mandatory AND unique.

通过获取数组中的 objectAtIndex:0 并更改其位置来访问对象属性:

Access the object by grabbing the objectAtIndex:0 in the array and change it's position property:

UserInfo *userInfoToBeEdited = [fetchRequestArray objectAtIndex:0];
[userInfoToBeEdit setValue:positionTextField.text forKey:@"position"];

在上述两种情况下,当您准备提交更改时,请记住调用CoreData的save方法。在调用保存之前,所做的更改仅保存在托管对象上下文中,这基本上是持久数据的暂存区。

In both cases above, remember to invoke CoreData's save method when you are ready to commit your changes. Before save is invoked your changes are only kept in your managed object context which is basically a scratch pad for your persistent data.

根据您的评论,我通常在AppDelegate中使用以下保存方法(直接从Apple模板复制/粘贴)

As per your comment, I usually have the save method below in my AppDelegate (copy/paste directly from Apple template)

- (void)saveContext
{
    error = nil;
    NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
    if (managedObjectContext != nil)
    {
        if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error])
        {
            [self seriousErrorAlert];
        } 
    }
}

然后在需要的时候从任何视图控制器保存更改,我只需获取对我的AppDelegate的引用并将其触发:

And then whenever I need to save changes, from any view controller I simply grab a reference to my AppDelegate and fire it off:

AppDelegate *theDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
[theDelegate saveContext];

这篇关于核心数据编辑属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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