我尝试扩展 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);
有用
我在这里发现了类似的问题,但没有解决方案。谢谢你的帮助。