NSManagedObject子类-无法识别的选择器发送到实例

问题描述 投票:2回答:2

我有一个从xcdatamodeld生成的核心数据类ZSShotCD(是的,我已经在模型中正确设置了该类)。我不想在其中放置任何自定义方法,因为可能需要在某个时候重新生成,因此我将其子类化为ZSShot。以下是一些相关的位:

首先,生成的类:

#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>

@interface ZSShotCD : NSManagedObject

@property (nonatomic, retain) NSString * shotDescription;
@property (nonatomic, retain) NSString * shotType;

@end

。m文件就是您所期望的,为属性提供了一堆@dynamic声明。我一点都没弄乱。

现在是子类-ZSShot.h:

#import <Foundation/Foundation.h>
#import "ZSShotCD.h"

@interface ZSShot : ZSShotCD
- (NSString *)MainText;
@end

和.m文件:

#import "ZSShot.h"

@implementation ZSShot

- (NSString *)MainText
{
    NSString *mainText = [NSString stringWithFormat:@"%@ %@", [self valueForKey:@"shotType"], [self valueForKey:@"shotDescription"]];
    return mainText;
}

[当我尝试在ZSShot实例上调用MainText方法时,如下所示:

cell.shotDescriptionLabel.text = [item MainText];

我得到:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ZSShotCD MainText]: unrecognized selector sent to instance 0x8d130c0'

该实例(单元)对实体中定义的属性没有问题,(它可以很好地从Core Data中提取数据),而我使用的代码与在其他实体上构建的类上的代码基本相同-唯一区别在于我尝试使用子类中定义的方法。

任何人都可以阐明这一点吗?

ios objective-c core-data
2个回答
4
投票

您需要为xxx.xcdatamodeld中的实体设置类,如下所示:“在此处输入图像描述”

((UserInfo是UserBase的子类,它是NSManagedObject类。)

否则,您将获得没有调用方法的基类的实例。


0
投票
enter code here

Blockquote enter link description here

;;kl;ljjljjljljljlj

**

© www.soinside.com 2019 - 2024. All rights reserved.