属性要求在NSManagedObject子类中定义方法

4

我有这样的一些协议:

@protocol UserProtocol <NSObject>

@property (nonatomic, strong) NSNumber *uid;
@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSNumber *rating;

@end

然后我创建了一些实际的类来实现它:

@interface User : NSObject <UserProtocol>

@end

现在我需要另一个使用CoreData的实现,因此我创建了CDUser实体(Xcode也为其生成类别):
// CDUser.h
@interface CDUser : NSManagedObject <UserProtocol>

@end

// CDUser+CoreDataProperties.h
@interface CDUser (CoreDataProperties)

@property (nullable, nonatomic, retain) NSNumber *uid;
@property (nullable, nonatomic, retain) NSString *name;
@property (nullable, nonatomic, retain) NSNumber *rating;

@end

// CDUser+CoreDataProperties.m
@implementation CDUser (CoreDataProperties)

@dynamic uid;
@dynamic name;
@dynamic rating;

@end
CDUser 实际上实现了 UserProtocol,但我对所有属性都有以下警告:

属性 'uid' 要求定义方法 'uid' - 使用 @synthesize、@dynamic 或在此类实现中提供方法实现

如果我在 CDBook.m 中再次添加 @dynamic uid;,那么我会收到以下错误:

在类实现中无法实现在类别“CoreDataProperties”中声明的属性

如何以正确的方式解决这些警告?

1个回答

3
由于CDUser没有实现该协议,因此应在类别上使用协议。
@interface CDUser : NSManagedObject

@end

// CDUser+CoreDataProperties.h
@interface CDUser (CoreDataProperties)  <UserProtocol>

@property (nullable, nonatomic, retain) NSNumber *uid;
@property (nullable, nonatomic, retain) NSString *name;
@property (nullable, nonatomic, retain) NSNumber *rating;

@end

1
此代码是由Xcode自动生成的,因此在下一次生成时,您的协议定义将被删除。 - Bogdan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接