关于ios:类类别中的方法不作为实例方法工作

Method in class category works not as instance method

在我的项目中,我有两个NSManageObjects(生成的核心数据)。对于这两个对象,我都用名为containsToday的实例方法创建了一个类别。

问题是只有对象ListElement的方法有效。在调用对象NamedRange的方法时,我得到了(与类方法相同的方法):

-[NSManagedObject containsToday]: unrecognized selector sent to instance ...

下面是一些代码:

ListElement:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>

@class ListImage, TLList;

@interface ListElement : NSManagedObject

@property (nonatomic, retain) NSString * agn;
@property (nonatomic, retain) NSString * aid;
@property (nonatomic, retain) NSString * date;
@property (nonatomic, retain) NSString * iid;
@property (nonatomic, retain) NSNumber * indx;
@property (nonatomic, retain) NSNumber * mark;
@property (nonatomic, retain) NSString * oid;
@property (nonatomic, retain) NSString * tcd;
@property (nonatomic, retain) NSString * ted;
@property (nonatomic, retain) NSNumber * cxl;
@property (nonatomic, retain) NSString * oti;
@property (nonatomic, retain) ListImage *listImage;
@property (nonatomic, retain) TLList *tlList;

@end

NamedRange:

1
2
3
4
5
6
7
8
9
10
11
12
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>


@interface NamedRange : NSManagedObject

@property (nonatomic, retain) NSString * tid;
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSString * sta;
@property (nonatomic, retain) NSString * end;

@end

ListElement(添加项):

1
2
3
4
5
6
7
#import"ListElement.h"

@interface ListElement (Additions)

- (BOOL) containsToday;

@end

名称范围(添加):

1
2
3
4
5
6
7
8
#import"NamedRange.h"

@interface NamedRange (Additions)

#warning TODO - why does this not work as instance method (for ListElement it works)?
+ (BOOL) containsToday:(NamedRange*)nr;
- (BOOL) containsToday;
@end


您可能没有在数据模型中正确设置实体类。消息表明它正试图调用类NSManagedObject的对象上的方法,而不是派生类的实例。

在Xcode中,打开数据模型,选择ListElement实体,然后打开右侧的数据模型检查器(在实用程序栏中),并检查实体类是否正确。如果我是正确的,我想您会发现它目前仍然设置为nsmanagedObject。