【发布时间】:2013-07-09 10:01:50
【问题描述】:
我尝试扩展 NSManagedObject。 我使用 XCode 创建了 MyBox.m 和 MyBox.h(直接来自 xcdatamodel 文件)。
然后我修改了这些文件:
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
@interface MyBox : NSManagedObject
@property (nonatomic, retain) NSDate * endDate;
@property (nonatomic, retain) NSNumber * globalId;
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSDate * startDate;
-(NSString *)sayHello;
@end
和
#import "MyBox.h"
@implementation MyBox
@dynamic endDate;
@dynamic globalId;
@dynamic name;
@dynamic startDate;
-(NSString *)sayHello {
return @"hello";
}
@end
我可以取出所有我的盒子
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"MyBox" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSMutableArray *myBoxes = [context executeFetchRequest:fetchRequest error:&error];
但后来我打电话
MyBox *myBox = [myBoxes objectAtIndex:indexPath.row];
[myBox sayHello];
它编译但我得到了
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSManagedObject sayHello]: unrecognized selector sent to instance 0x8e73fc0'
如果我只读取类似的值
NSLog(@"%@", myBox.name);
有效
我在这里发现了类似的问题,但没有解决方案。 感谢您的帮助。
【问题讨论】:
-
您可能忘记在 coredata 编辑器中将实体的类设置为
MyBox。 -
我在 coredata 编辑器中设置了 Entity->Name = Entity->Class = "MyBox"。我必须设置一些其他属性吗?
-
嗯,这很奇怪。不,这应该足够了......你可能有多个版本吗?
-
我同意阿拉丁的观点。如果实体类设置为 MyBox,那么这应该可以工作。但错误消息明确指出
myBox是only NSManagedObject。也许在获取并检查类之后直接 NSLogmyBoxes数组。 -
调用
[myBox sayHello]方法的类与Core Data类是否在同一个项目中?
标签: objective-c core-data nsmanagedobject