objective-c - [NSManagedObject 打招呼] : unrecognized selector sent to instance 0x

标签 objective-c core-data 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

#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/

相关文章:

core-data - 如何根据相关对象转换 SwiftUI 获取请求结果?

cocoa - NSManagedObjectModel 中合并模型的跨模型关系?

objective-c - 在 Core Data 之外重用 NSManagedObjects 的结构

iOS 如何在最大震级未知时为热/冷频谱图选择颜色?

ios - 在CoreData中存储 "Item-ItemDetails"的最佳方式

objective-c - 为什么数组按引用传递,而结构在 C 中隐式按值传递?

iphone - 核心数据交叉引用两种关系

objective-c - NSManagedObjectContext在Core Data应用程序(iOS)中返回nil

iPhone UIDocumentInteractionController PresentOpenInMenuFromRect 取消按钮没有响应

objective-c - WebKit 10.8 : How to programmatically set the value of an input[type=file] field?