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

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

问题描述

我有一个NSManagedObject子类,由XCode模型接口创建。

这个类有一些NSString和NSNumber成员和一个NSDate成员。



当我尝试设置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'



日期参数与其他参数一样,但不是

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

这是

  @property(nonatomic,retain)NSDate * date; 

Btw, Item 赋值on只是一个常规的 [[Item alloc] init] ,没有相关的上下文或任何东西。



认为我的NSDate *是错误的,然后我尝试分配它[NSDate日期],甚至没有。

解决方案

您可以' t实例化一个 NSManagedObject 子类,不带相关的 NSManagedObjectContext (你可以显示,但结果几乎肯定不是你想要的)。



日志的第一行提示:

  2009-10-12 21:53:32.228 xxx [2435:20b]无法调用NSManagedObject类'Item'上的指定初始化器



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


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

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;  

it's a

@property (nonatomic, retain) NSDate *date;

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

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

Any ideas?

解决方案

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

The first line of the log hints at this:

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

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天全站免登陆