我试图扩展NSManagedObject。使用XCode,我从xcdatamodel文件直接创建了MyBox.m和MyBox.h文件。
然后我修改了这些文件:
#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);
它起作用了
我在这里发现了类似的问题,但没有解决方法。 感谢您的帮助。
MyBox
。 - Alladinian[myBox sayHello]
方法的类是否与Core Data类在同一个项目中? - ChrisH