[NSManagedObject sayHello]:无法识别的选择器发送到实例0x

ste*_*anr 10 core-data objective-c nsmanagedobject

我尝试扩展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
Run Code Online (Sandbox Code Playgroud)

#import "MyBox.h"
@implementation MyBox

@dynamic endDate;
@dynamic globalId;
@dynamic name;
@dynamic startDate;

-(NSString *)sayHello {
    return @"hello";
}  

@end
Run Code Online (Sandbox Code Playgroud)

我可以获取所有myBoxes

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription
                                   entityForName:@"MyBox" inManagedObjectContext:context];
    [fetchRequest setEntity:entity];

NSMutableArray *myBoxes = [context executeFetchRequest:fetchRequest error:&error];
Run Code Online (Sandbox Code Playgroud)

但后来我打来电话

MyBox *myBox = [myBoxes objectAtIndex:indexPath.row];    
    [myBox sayHello];
Run Code Online (Sandbox Code Playgroud)

它编译然后我得到

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSManagedObject sayHello]: unrecognized selector sent to instance 0x8e73fc0'
Run Code Online (Sandbox Code Playgroud)

如果我只读一个像这样的值

NSLog(@"%@", myBox.name);
Run Code Online (Sandbox Code Playgroud)

有用

我在这里发现了类似的问题,但没有解决方案 谢谢你的帮助.

Ale*_*nin 20

我刚刚遇到同样的问题.通过将类名更改为我的NSManagedObject子类的名称来解决它myApp.xcdatamodeld -> configurations -> default -> entities -> myEntity.


Dre*_*its 3

假设您已在实体上正确设置类名MyBox,我猜测该应用程序具有较旧版本的核心数据托管对象模型。清理您的构建并删除模拟器/设备上的应用程序以进行良好的测量。为了 100% 确定,还要删除您的派生数据文件夹。

如果之后不起作用,我敢打赌你没有正确设置实体类名称。打印出来NSEntityDescription并确保它符合您的期望。