Cocoa-Touch、NSManagedObject:设置属性时出现异常 [英] Cocoa-Touch, NSManagedObject: Exception when setting a property

查看:18
本文介绍了Cocoa-Touch、NSManagedObject:设置属性时出现异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 NSManagedObject 子类,由 XCode 模型接口创建.
这个类有一些 NSString 和 NSNumber 成员,以及一个 NSDate 成员.

I have a NSManagedObject subclass, created by the XCode model interface.
This class has some NSString and NSNumber members, and a NSDate member.

当我尝试设置 NSDate 成员时,出现以下异常:
2009-10-12 21:53:32.228 xxx[2435:20b] 无法在 NSManagedObject 类Item"上调用指定的初始化程序
2009-10-12 21:53:32.228 xxx[2435:20b] *** -[Item setDate:]:无法识别的选择器发送到实例 0x3f7ed30
2009-10-12 21:53:32.229 xxx[2435:20b] *** 由于未捕获的异常NSInvalidArgumentException"而终止应用程序,原因:*** -[Item setDate:]:无法识别的选择器发送到实例 0x3f7ed30'

When I try to set the NSDate member, I get the following exception:
2009-10-12 21:53:32.228 xxx[2435:20b] Failed to call designated initializer on NSManagedObject class 'Item'
2009-10-12 21:53:32.228 xxx[2435:20b] *** -[Item setDate:]: unrecognized selector sent to instance 0x3f7ed30
2009-10-12 21:53:32.229 xxx[2435:20b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[Item setDate:]: unrecognized selector sent to instance 0x3f7ed30'

日期参数和其他参数一样,只是不是一个

The date parameter is just like the others, except that instead of being a

@property (nonatomic, retain) NS{String,Number}* propname;  

这是一个

@property (nonatomic, retain) NSDate *date;

顺便说一句,我分配的 Item 实例只是一个常规的 [[Item alloc] init],没有关联的上下文或任何东西.

Btw, the Item instance I'm assigning on is just a regular [[Item alloc] init], no associated context or anything.

首先我认为我的 NSDate* 有问题,然后我尝试分配它 [NSDate date],甚至为零.它仍然崩溃.

First I thought my NSDate* was faulty, then I tried assigning it [NSDate date], and even nil. It still crashes.

有什么想法吗?

推荐答案

你不能在没有关联的 NSManagedObjectContext 的情况下实例化 NSManagedObject 子类(你可以 正如你所展示的,但结果几乎肯定不是你想要的).

You can't instantiate an NSManagedObject subclass without an associated NSManagedObjectContext (well you can as you've shown, but the results will almost certainly not be what you want).

日志的第一行提示:

2009-10-12 21:53:32.228 xxx[2435:20b] Failed to call designated initializer on NSManagedObject class 'Item'

所有 Objective-C 类(按照约定)都有一个指定的初始化器,这是必须调用的初始化器方法,可以显式调用,也可以通过其他方便的初始化器调用.对于 NSManagedObject,这是 -[NSManagedObject initWithEntity:insertIntoManagedObjectContext:].未能导致指定的初始化程序会导致未定义的,并且可能不正确的行为,因为不能保证实例被正确初始化.我猜想 NSManagedObject 初始值设定项会设置机制以支持 @synthesize 对实体属性的属性访问.如果没有这种机制,实例可能认为它无法响应 @synthesize 的调用,而您对 setData: 的调用将导致选择器未找到错误.

All Objective-C classes have (by convention) a designated initializer, which is the initializer method that must be called, either explicitly or via an other convenience initializer. In the case of NSManagedObject this is -[NSManagedObject initWithEntity:insertIntoManagedObjectContext:]. Failure to cause the designated initializer leads to undefined, and likely incorrect, behavior because the instance is not guaranteed to be properly initialized. I would guess that the NSManagedObject initializer sets up the machinery to support @synthesize'd property access for the Entity's attributes. Without this machinery, the instance may not think it can respond to the @synthesize'd calls and your call to setData: will cause a selector not found error.

这篇关于Cocoa-Touch、NSManagedObject:设置属性时出现异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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