我有一个代表 rss 文章的简单模型类,称为 RSSEntry。
现在我想开始使用这个模型类处理核心数据,但我没有在创建项目时选中“使用核心数据”复选框。
这是类:
#import <Foundation/Foundation.h>
@interface FRSSEntry : NSObject{
NSString *_blogTitle;
NSString *_articleTitle;
NSString *_articleUrl;
NSDate *_articleDate;
NSString *_articleImageUrl;
NSString *_content;
}
@property (copy) NSString *blogTitle;
@property (copy) NSString *articleTitle;
@property (copy) NSString *articleUrl;
@property (copy) NSDate *articleDate;
@property (copy) NSString *articleImageUrl;
@property (copy) NSString *content;
- (id)initWithBlogTitle:(NSString*)blogTitle articleTitle:(NSString*)articleTitle articleUrl:(NSString*)articleUrl articleDate:(NSDate*)articleDate articleImageUrl:(NSString *)imageUrl andContent:(NSString *)content;
@end
实现是:
#import "FRSSEntry.h"
@implementation FRSSEntry
@synthesize blogTitle = _blogTitle;
@synthesize articleTitle = _articleTitle;
@synthesize articleUrl = _articleUrl;
@synthesize articleDate = _articleDate;
@synthesize articleImageUrl = _articleImageUrl;
@synthesize content = _content;
- (id)initWithBlogTitle:(NSString*)blogTitle articleTitle:(NSString*)articleTitle articleUrl:(NSString*)articleUrl articleDate:(NSDate*)articleDate articleImageUrl:(NSString *)imageUrl andContent:(NSString *)content
{
if ((self = [super init])) {
_blogTitle = [blogTitle copy];
_articleTitle = [articleTitle copy];
_articleUrl = [articleUrl copy];
_articleDate = [articleDate copy];
_articleImageUrl = [imageUrl copy];
_content = [content copy];
}
return self;
}
@end
很简单。现在我该如何转换它以便将其用作核心数据实体?
最佳答案
因此,要将模型类转换为 NSManagedObject
子类,您必须删除实例变量声明。然后用@dynamic 替换所有@synthesize 语句。这告诉编译器 CoreData 将为这些属性提供实现,因此它可以在那里发挥它的魔力。您拥有的自定义初始化程序也需要删除,因为 NSManagedObject
对象以不同的方式初始化。
代码看起来像
#import <Foundation/Foundation.h>
@interface FRSSEntry : NSManagedObject
@property (copy) NSString *blogTitle;
@property (copy) NSString *articleTitle;
@property (copy) NSString *articleUrl;
@property (copy) NSDate *articleDate;
@property (copy) NSString *articleImageUrl;
@property (copy) NSString *content;
@end
-
#import "FRSSEntry.h"
@implementation FRSSEntry
@dynamic blogTitle;
@dynamic articleTitle;
@dynamic articleUrl;
@dynamic articleDate;
@dynamic articleImageUrl;
@dynamic content;
@end
你通常用类似的东西初始化它们
// Get the entity description
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"FRSSEntry" inManagedObjectContext:context];
// Insert a new YourModelObject into the context
ReceivedMessage *newMessage = [[FRSSEntry alloc] initWithEntity:entityDescription insertIntoManagedObjectContext:context];
您可以使用自定义初始化器,但您必须调用 [super initWithEntity:entityDescription insertIntoManagedObjectContext:context]
。带有所有这些参数的初始化程序会变得非常长,所以我建议您在初始化对象后设置每个属性。
从您的回复来看,您刚刚开始 CoreData 集成。创建 NSManagedObject
子类只是处理核心数据时的冰山一角。 CoreData 是一个庞大而复杂的框架,所以我建议您先阅读 https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CoreData/cdProgrammingGuide.html . Stack Overflow 也充满了关于这个主题的问题和很好的答案。
我建议您研究的另一件事是 MagicalRecord
。这是一个很棒的库,可以使一些繁琐的任务变得非常简单:https://github.com/magicalpanda/MagicalRecord
关于ios - 如何将 NSObject 模型类转换为 NSManagedObject 的子类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27654258/