CoreData:错误:无法在NSManagedObject类'Class Name'上调用指定的初始值设定项

时间:2013-07-12 06:48:59

标签: core-data ios6 restkit restkit-0.20

我试图使用CoreData和RestKit 0.20.0我得到了CoreData错误。

// Response.h

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


@interface Response : NSManagedObject

@property (nonatomic, retain) NSString * status;
@property (nonatomic, retain) NSString * responseCode;
@property (nonatomic, retain) NSString * responseMsg;
@property (nonatomic, retain) NSString * error;
@property (nonatomic, retain) NSString * statisticsDetails;

@end

和Implementaion文件是

// Response.m

导入“Response.h”

@implementation Response

@dynamic error;
@dynamic responseCode;
@dynamic responseMsg;
@dynamic statisticsDetails;
@dynamic status;

@end

我的ViewController代码是

// ViewController.m

#import "ViewController.h"
#import "StatisticsDetails.h"
#import "TopProjects.h"
#import <RestKit.h>

- (void)viewDidLoad
{
[super viewDidLoad];

 RKObjectManager *manager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:@"http://mylocalUrl.com/Port"]];

/*managed object model */
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"Model" withExtension:@"momd"];
NSManagedObjectModel *managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
 NSLog(@"managed object model: %@", managedObjectModel);

/* managed object store */
RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];
manager.managedObjectStore = managedObjectStore;
NSLog(@"managed object store: %@", managedObjectStore);

/* persistent store*/
NSError *error;
NSString *storePath = [RKApplicationDataDirectory() stringByAppendingPathComponent:@"Model.sqlite"];
NSPersistentStore *persistentStore = [managedObjectStore addSQLitePersistentStoreAtPath:storePath fromSeedDatabaseAtPath:nil withConfiguration:nil options:nil error:&error];
if (error) {
        NSLog(@"unresolved error , ");
     NSAssert(persistentStore, @"Failed to add persistent store with error: %@", error);

}

/* managed object contexts*/


[managedObjectStore createManagedObjectContexts];

/* entity mapping */

RKEntityMapping *responseEntityMapping =[RKEntityMapping mappingForEntityForName:@"Response" inManagedObjectStore:managedObjectStore];

[responseEntityMapping addAttributeMappingsFromDictionary:@{
 @"status" : @"status",
 @"responseCode":@"responseCode",
 @"responseMsg":@"responseMsg",
 @"error":@"error",
 @"statisticsDetails":@"statisticsDetails"
 }];

RKResponseDescriptor *ResponseDescriptor = [ RKResponseDescriptor responseDescriptorWithMapping:responseEntityMapping pathPattern:nil keyPath:@"" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

[[RKObjectManager sharedManager] getObjectsAtPath:@"getStatistics/4" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
          /* NSLog(@"mapping result: %d", [mappingResult array].count);

     NSLog(@"Mapping Result: %@", mappingResult.array);
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
          RKLogError(@"Operation failed with error: %@", error);     
}];

错误是

2013-07-12 12:35:09.098 Dashboard[85919:5403] CoreData: error: Failed to call designated initializer on NSManagedObject class 'ResponseClas'  

请帮帮我。提前致谢

1 个答案:

答案 0 :(得分:2)

您正在创建RKObjectManager个实例,但未向其提供managedObjectStore的引用,这很可能是问题的原因。