我尝试扩展 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
我可以获取所有 myBoxes
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);
有效
我在这里发现了类似的问题,但没有解决方案。 感谢您的帮助。
最佳答案
我也遇到了同样的问题。通过在 myApp.xcdatamodeld -> 配置 -> 默认 -> 实体 -> myEntity
中将类名更改为我的 NSManagedObject 子类的名称来解决它。
关于objective-c - [NSManagedObject 打招呼] : unrecognized selector sent to instance 0x,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17545400/