核心数据保存错误(NSValidationErrorKey,Cocoa错误1570)保存NSDate
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
为了强化迈克尔的答案,您可以在检查器中检查您的实体属性。您的其中一项可能会被意外地视为可选,也可能不是。或者,如果您像我一样,您的“删除规则”可能已设置为“无效”,这使得我的实体的关系属性在运行时
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 toCascade
solved the problem.Entity Inspector - Delete Rule
cocoa 错误
1570
表示未填写必填字段。在这种情况下,您有两个
nil
属性:Voedsel
和datum
。我在您的代码中看到:
检查
geselecteerdVoedsel
和selDatum
不为零,或者它们被过度释放并最终为零。如果它们是可选数据(但我不这么认为),请将它们定义为 coredata 中的可选数据。
希望这有帮助,
The cocoa error
1570
means that mandatory fields are not filled in.In this case, your have two attribues that are
nil
:Voedsel
anddatum
.I see in your code :
Check that
geselecteerdVoedsel
andselDatum
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,
正如迈克尔 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.
您的日志记录将如下所示:
这意味着您对实体的一个(或多个)属性进行了未提交的更改,其中您告诉它不是可选的,但您将其保留为可选。
要找出您未能为属性设置值的实体,请在日志记录中查找:
要查找属性,请搜索:
在代码中的某个位置您忘记为给定实体的该属性设置值。
Your logging would look like this:
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:
To find out the property, search for:
Somewhere in your code you forget to set a value for that property for the given entity.
当我从其他
xcdatamodeld
文件复制实体时,我遇到了这个问题。失去了逆属性,就是这个原因。
I had this issue when I copied entities from other
xcdatamodeld
file.They lost inverse attributes, so that was the reason.
与日期并不真正相关,但由于错误,我分享它,因为这个问题有更多观点:
在我的例子中,我将
BOOL
属性直接设置为YES
或 < code>NO,但你应该使用才能使其正常工作。
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 asYES
orNO
, but you should usein order to make it work.