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

发布于 2024-11-19 16:39:14 字数 1478 浏览 3 评论 0原文

我在 Xcode 中保存到核心数据对象时遇到错误。

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.)

一小段代码片段:

        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]) {
...

也是 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

因此您可以看到我将 NSDate 放入“datum”变量中。但在执行时我仍然收到错误。

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

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

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:

@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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

只等公子 2024-11-26 16:39:15

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

实体检查器 - 删除规则

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 Nil at 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 Cascade solved the problem.

Entity Inspector - Delete Rule

旧梦荧光笔 2024-11-26 16:39:14

cocoa 错误 1570 表示未填写必填字段。
在这种情况下,您有两个 nil 属性:Voedseldatum

我在您的代码中看到:

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

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

希望这有帮助,

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

I see in your code :

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

Check that geselecteerdVoedsel and selDatum are 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.

Hope this help,

没有伤那来痛 2024-11-26 16:39:14

正如迈克尔 A 所说。检查你的属性值不为零。
有两种方法可以消除这些错误。
情况 1:如果 2 个属性是必需的

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

对于数据模型案例 2 :
通过选择可选的复选标记,在属性检查器中将这些属性设置为可选。

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

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

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

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

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

凉城 2024-11-26 16:39:14

您的日志记录将如下所示:

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

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

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

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

要查找属性,请搜索:

NSValidationErrorKey=YOURPROPERTYISHERE

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

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.

情丝乱 2024-11-26 16:39:14

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

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

∞觅青森が 2024-11-26 16:39:14

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

在我的例子中,我将 BOOL 属性直接设置为 YES 或 < code>NO,但你应该使用

NSNumber numberWithBOOL

才能使其正常工作。

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 BOOL property directly as YES or NO, but you should use

NSNumber numberWithBOOL

in order to make it work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文