这是我第一次尝试完成核心数据,并且遇到了一些问题。我的应用程序中有一个xml解析器,我一直在尝试将所有信息添加到核心数据中,但在启动时一直崩溃。我将我的核心数据设置为1个实体作为“主题”。在那个实体中,我有15个属性。下面是我创建.CoreData后为ManagedObject创建的自动文件xcode。
Themes.h
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
@interface Themes : NSManagedObject
@property (nonatomic, retain) NSString * themeName;
@property (nonatomic, retain) NSString * themeArtist;
@property (nonatomic, retain) NSString * themeImage;
@property (nonatomic, retain) NSString * themeDescription;
@property (nonatomic, retain) NSString * twitterName;
@property (nonatomic, retain) NSString * themePrice;
@property (nonatomic, retain) NSString * screenshots;
@property (nonatomic, retain) NSString * cydiaLink;
@property (nonatomic, retain) NSString * themeVersion;
@property (nonatomic, retain) NSString * deciption;
@property (nonatomic, retain) NSString * repo;
@property (nonatomic, retain) NSString * hd;
@property (nonatomic, retain) NSString * sd;
@property (nonatomic, retain) NSString * ipad;
@end
#import "Themes.h"
@implementation Themes
@dynamic themeName;
@dynamic themeArtist;
@dynamic themeImage;
@dynamic themeDescription;
@dynamic twitterName;
@dynamic themePrice;
@dynamic screenshots;
@dynamic cydiaLink;
@dynamic themeVersion;
@dynamic deciption;
@dynamic repo;
@dynamic hd;
@dynamic sd;
@dynamic ipad;
-(void)setThemeName:(NSString *)themeName{
self.themeName = [themeName copy];
}
@end
这是我的xml Parser文件,其中应该发生所有魔法。总的来说,一切都很好用我的解析器,就在我做的时候
“themesObjects =(主题*)[NSEntityDescription
insertNewObjectForEntityForName:themesObjects.themeName“
inManagedObjectContext:managedObjectContext];“
应用程序将崩溃我假设因为我没有正确地将对象插入到coredata中。
#import <Foundation/Foundation.h>
#import "ThemeParseObject.h"
#import "Themes.h"
@class ThemeParseObject;
@interface ThemeXMLParser : NSObject <NSXMLParserDelegate> {
NSMutableData *recivedData;
NSMutableArray *themes;
NSMutableString *currentNodeContent;
NSXMLParser *parser;
Themes *themesObjects;
NSManagedObjectContext *managedObjectContext;
NSMutableArray *themeArray;
ThemeParseObject *currentTheme;
}
@property (readonly, retain) NSMutableArray *themes;
@property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, retain) NSMutableArray *themeArray;
-(id) loadXMLByURL:(NSString *)urlString;
@end
#import "ThemeXMLParser.h"
@implementation ThemeXMLParser
@synthesize themes;
@synthesize managedObjectContext;
@synthesize themeArray;
-(id) loadXMLByURL:(NSString *)urlString{
themesObjects = [[Themes alloc]init];
managedObjectContext = [[NSManagedObjectContext alloc] init];
themes = [[NSMutableArray alloc] init];
NSURL *url = [NSURL URLWithString:urlString];
NSData *data = [[NSData alloc] initWithContentsOfURL:url];
parser = [[NSXMLParser alloc] initWithData:data];
parser.delegate = self;
[parser parse];
return self;
}
- (void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementname
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
attributes:(NSDictionary *)attributeDict
{
if ([elementname isEqualToString:@"theme"])
{
currentTheme = [ThemeParseObject alloc];
}
}
- (void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
NSManagedObjectContext *context = [self managedObjectContext];
NSManagedObject *ObjectsToInsert = [NSEntityDescription
insertNewObjectForEntityForName:@"Themes"
inManagedObjectContext:context];
if ([elementName isEqualToString:@"name"]) {
currentTheme.themeNameString = currentNodeContent;
themesObjects.themeName = currentTheme.themeNameString;
[ObjectsToInsert setValue:themesObjects.themeName forKey:@"themeName"];
}
if ([elementName isEqualToString:@"creator"]) {
currentTheme.themeCreator = currentNodeContent;
// themesObjects = (Themes *)[NSEntityDescription
insertNewObjectForEntityForName:@"themeArtist"
inManagedObjectContext:managedObjectContext];
}
if ([elementName isEqualToString:@"price"]) {
currentTheme.themePrice = currentNodeContent;
//themesObjects = (Themes *)[NSEntityDescription
insertNewObjectForEntityForName:@"themePrice"
inManagedObjectContext:managedObjectContext];
}
if ([elementName isEqualToString:@"twitter"]) {
currentTheme.creatorTwitterName = currentNodeContent;
//themesObjects = (Themes *)[NSEntityDescription
insertNewObjectForEntityForName:@"twitterName"
inManagedObjectContext:managedObjectContext];
}
if ([elementName isEqualToString:@"link"]) {
currentTheme.cydiaLink = currentNodeContent;
//themesObjects = (Themes *)[NSEntityDescription
insertNewObjectForEntityForName:@"cydiaLink"
inManagedObjectContext:managedObjectContext];
}
if ([elementName isEqualToString:@"deciption"]) {
currentTheme.deciption = currentNodeContent;
//themesObjects = (Themes *)[NSEntityDescription
insertNewObjectForEntityForName:@"deciption"
inManagedObjectContext:managedObjectContext];
}
if ([elementName isEqualToString:@"screenshots"]) {
currentTheme.screenShots = currentNodeContent;
}
if ([elementName isEqualToString:@"promo"]) {
currentTheme.tblViewImage = currentNodeContent;
}
if ([elementName isEqualToString:@"description"]) {
currentTheme.themeDescription = currentNodeContent;
}
if ([elementName isEqualToString:@"version"]) {
currentTheme.themeVersion = currentNodeContent;
// NSLog(@"version: %@", currentNodeContent);
}
if ([elementName isEqualToString:@"repo"]) {
currentTheme.themeRepo = currentNodeContent;
// NSLog(@"repo: %@", currentNodeContent);
}
if ([elementName isEqualToString:@"HD"]) {
currentTheme.HD = currentNodeContent;
// NSLog(@"HD: %@", currentNodeContent);
}
if ([elementName isEqualToString:@"SD"]) {
currentTheme.SD = currentNodeContent;
// NSLog(@"SD: %@", currentNodeContent);
}
if ([elementName isEqualToString:@"iPad"]) {
currentTheme.ipad = currentNodeContent;
//NSLog(@"iPad: %@", currentNodeContent);
}
if ([elementName isEqualToString:@"iPhoneScreenshots"]) {
currentTheme.fullScreenShots = currentNodeContent;
// themesObjects = (Themes *)[NSEntityDescription
insertNewObjectForEntityForName:@"screenshots"
inManagedObjectContext:managedObjectContext];
// NSLog(@"fullScreenShots: %@", currentNodeContent);
}
if ([elementName isEqualToString:@"theme"])
{
[themes addObject:currentTheme];
[currentTheme release];
currentTheme = nil;
[currentNodeContent release];
currentNodeContent = nil;
}
}
- (void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
currentNodeContent = (NSMutableString *) [string
stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}
这是应用崩溃时记录的内容
2012-01-06 08:02:18.619 ThemeCatcher[2667:207] CoreData: error: Failed to call
designated initializer on NSManagedObject class 'Themes'
2012-01-06 08:02:19.000 ThemeCatcher[2667:207] -[Themes themeName]: unrecognized
selector sent to instance 0x87072d0
如果有人有任何建议或想法,我可以从这里去,那将是非常棒的。如果这看起来好像很多代码,我很抱歉,但我觉得让别人知道代码里面究竟发生了什么是非常的...谢谢你,我非常感谢任何帮助!
答案 0 :(得分:1)
themesObjects = [[Themes alloc]init];
managedObjectContext = [[NSManagedObjectContext alloc] init];
我认为这两行有两个错误。首先,您没有在主题初始化程序中使用NSManagedObject的指定初始化程序。我知道这有两个原因:a)这正是错误信息所说的; b)初始化程序无法知道要使用的上下文,因为您尚未在该点创建上下文。 designated initializer for NSManagedObjectContext是:
-initWithEntity:insertIntoManagedObjectContext:
但是,通常的做法是使用NSEntityDescription的+ insertNewObjectForEntityForName:inManagedObjectContext:
便捷方法来创建新对象并将其添加到上下文中。无论哪种方式,您都需要使用其中之一。
第二个问题是,在创建Themes对象时,您没有托管对象上下文,并且在创建它时,您似乎没有正确设置上下文。您应该在创建后为上下文设置持久性存储协调器。
更新:第三个问题是当你写:
themesObjects = (Themes *)[NSEntityDescription insertNewObjectForEntityForName:themesObjects.themeName" inManagedObjectContext:managedObjectContext];
您需要确保模型具有与themeObjects.themeName
的值匹配的实体。使用从XML文件获取的数据来确定主题名称是不常见的,因为XML文件中的错误会导致错误,并且还因为它将XML的结构与核心数据模型紧密联系在一起。确保您不会将 XML实体的概念与核心数据实体混淆 - 这是两回事。核心数据实体是模型中不同类型的对象;例如,你可能有一个主题实体。