xcode 保存 NSDate 的核心数据保存错误(NSValidationErrorKey,Cocoa 错误 1570)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6667585/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 04:01:39  来源:igfitidea点击:

Core Data Save Error (NSValidationErrorKey, Cocoa error 1570) saving NSDate

xcodecore-datansdate

提问by Peter

I'm getting an error with saving to a Core data object in Xcode.

在 Xcode 中保存到 Core 数据对象时出现错误。

Xcode says that the error is in the NSDate variable 'datum' but I have tried almost everything. Error is:

Xcode 说错误出在 NSDate 变量 'datum' 中,但我几乎尝试了所有方法。错误是:

2011-07-12 18:01:29.068 WeekLijstje[3205:207] Core Data Save Error
NSValidationErrorKey    datum
NSValidationErrorPredicate  (null)

NSValidationErrorObject
<DagLijst: 0x6e2fcd0> (entity: DagLijst; id: 0x6e2fd30 <x-coredata:///DagLijst/t99F423FC-AAE9-4692-9264-EF0FF7A020572> ; data: {
    Voedsel = nil;
    datum = nil;
    hoeveelheid = 0;
    punten = 0;
})
NSLocalizedDescription:The operation couldn't be completed. (Cocoa error 1570.)

A small code snipet:

一个小代码片段:

        DagLijst *newDaglijst = [NSEntityDescription insertNewObjectForEntityForName:@"DagLijst" inManagedObjectContext:self.managedObjectContext];

        NSDate *selDatum = [NSDate date];
        newDaglijst.punten = [NSNumber numberWithInteger:10];
        newDaglijst.hoeveelheid = [NSNumber numberWithInt:100];
        newDaglijst.Voedsel = geselecteerdVoedsel;
        newDaglijst.datum = selDatum;
        NSError *error = nil;
        if (![newDaglijst.managedObjectContext save:&error]) {
...

Also the class of the DagLijst object:

还有 DagLijst 对象的类:

@interface DagLijst : NSManagedObject {
@private
}
@property (nonatomic, retain) NSDate * datum;
@property (nonatomic, retain) NSNumber * punten;
@property (nonatomic, retain) NSNumber * hoeveelheid;
@property (nonatomic, retain) Voedsel *Voedsel;

@end

So you can see that I put an NSDate into the 'datum' variable. But on execution I still get an error.

所以你可以看到我将一个 NSDate 放入 'datum' 变量中。但是在执行时我仍然收到错误。

回答by Michael A.

The cocoa error 1570means that mandatory fields are not filled in. In this case, your have two attribues that are nil: Voedseland datum.

可可错误1570意味着未填写必填字段。在这种情况下,您有两个属性nilVoedseldatum

I see in your code :

我在你的代码中看到:

newDaglijst.Voedsel = geselecteerdVoedsel;
newDaglijst.datum = selDatum;

Check that geselecteerdVoedseland selDatumare not nil or that they are overreleased and finish to be nil. If they are optional data (but I don't think so), define them as optional in coredata.

检查geselecteerdVoedselselDatum不是零或它们是否被过度释放并完成为零。如果它们是可选数据(但我不这么认为),请在 coredata 中将它们定义为可选数据。

Hope this help,

希望这有帮助,

回答by skkrish

as Michael A said. Check your attributes value are not nil. There are 2 alternatives to get rid of these error. Case 1:If 2 attributes are Required

正如迈克尔 A 所说。检查您的属性值是否为空。有 2 种替代方法可以消除这些错误。案例 1:如果 2 个属性是必需的

If the 2 attributes are required attributes then it is mandatory to check the values you are passing are not nil,It may happens sometimes,If u want to get out of these error u have to give default values for those attributes in attributes inspector of your Entity in data Model

如果这 2 个属性是必需属性,则必须检查您传递的值是否为空,有时可能会发生这种情况,如果您想摆脱这些错误,您必须在您的属性检查器中为这些属性提供默认值数据模型中的实体

Case 2: Set those attributes to optional in attribute inspector by selecting the check mark of optional.

情况 2:通过选择可选的复选标记,在属性检查器中将这些属性设置为可选。

Me too struggled for days to know these error. Hope it helps someone.

我也挣扎了好几天才知道这些错误。希望它可以帮助某人。

回答by J. Doe

Your logging would look like this:

您的日志记录如下所示:

Fatal error: 'try!' expression unexpectedly raised an error: 
Error Domain=NSCocoaErrorDomain Code=1560 "(null)" UserInfo={NSDetailedErrors=(
...

It means you have an uncommitted change for one (or more) property of a entity, in which you told it is NOT optional, but you left it optional.

这意味着您对实体的一个(或多个)属性进行了未提交的更改,您告诉它不是可选的,但您将其保留为可选的。

To find out which entity you failed to set value for a property, look for this in your logging:

要找出您未能为属性设置值的实体,请在您的日志记录中查找:

UserInfo={NSValidationErrorObject=<YOURENTITYISHERE: ...>

To find out the property, search for:

要查找该属性,请搜索:

NSValidationErrorKey=YOURPROPERTYISHERE

Somewhere in your code you forget to set a value for that property for the given entity.

在代码中的某个地方,您忘记为给定实体设置该属性的值。

回答by htafoya

Not really related with date, but with the error, I share it as this question has more views:

与日期并没有真正相关,但有错误,我分享它,因为这个问题有更多的观点:

In my case, I was setting a BOOLproperty directly as YESor NO, but you should use

就我而言,我将BOOL属性直接设置为YESor NO,但您应该使用

NSNumber numberWithBOOL

NSNumber numberWithBOOL

in order to make it work.

为了使它工作。

回答by Tim Isenman

To reinforce Michael's answer, you can check your Entity properties in the inspector. One of your items may be accidentally considered Optional, or not. Or, if you're like me, your "Delete Rule" might have been set to "Nullify", which made my Entity's relationship property Nilat runtime. Because my object-to-be-deleted had a One-To-Many relationship with some other objects, it prevented the parent objects from being deleted. Changing it to Cascadesolved the problem.

为了加强迈克尔的回答,您可以在检查器中检查您的实体属性。您的其中一项可能会被意外地视为可选,也可能不是。或者,如果您像我一样,您的“删除规则”可能已设置为“无效”,这使我的实体Nil在运行时具有关系属性。因为我的待删除对象与其他一些对象存在一对多关系,所以它阻止了父对象被删除。更改它以Cascade解决问题。

Entity Inspector - Delete Rule

实体检查器 - 删除规则

回答by Shmidt

I had this issue when I copied entities from other xcdatamodeldfile. They lost inverse attributes, so that was the reason.

当我从其他xcdatamodeld文件复制实体时遇到了这个问题。他们失去了逆属性,所以这就是原因。