iOS   发布时间:2022-03-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – 实体(null)不是密钥值编码兼容的密钥“标题”大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图让RestKit和CoreData一起工作.我越来越近,但我收到以下错误

CoreData: error: Failed to call designated initializer on NsmanagedObject class 'Book' 
*** TerminaTing app due to uncaught exception 'NSUnkNownKeyException',reason: 
    '[<Book 0x8454560> valueForUndefinedKey:]: the entity (null) is not key value coding-compliant for the key "title".'

在我看来,它成功地找到了我的Book类,并且它有一个title属性.我究竟做错了什么?

Books.xcdatamodel

Book
  title: String

我在localhost有一个网址:3000 / books / initial返回以下内容(JSON)

[{title:"one"},{title:"two"}]

我正在使用mogenerator来创建我的课程.我没有在Book中添加任何东西,但是_Book显然已经定义了title属性.

最后,这是我用来加载请求的代码.

RKObjectManager* objectManager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:@"http://localhost:3000/"]];
RKManagedObjectStore* objectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:self.model];
objectManager.managedObjectStore = objectStore;

// Mappings
RKEntitymapping *bookMapping = [RKEntitymapping mappingForEntityForName:@"Book" inManagedObjectStore:objectStore];
[bookMapping addAttributeMappingsFromArray:@[@"title"]];

RKResponseDescriptor * responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:bookMapping pathPattern:@"books/initial/" keyPath:nil statusCodes:rKStatusCodeIndexSetForClass(RKStatusCodeClasssuccessful)];

[objectManager addResponseDescriptor:responseDescriptor];

// Send request
[objectManager getObjectsAtPath:@"/books/initial/" parameters:nil success:^(RKObjectrequestOperation * operation,RKMappingResult *mappingResult) {
    NSLog(@"succesS");
} failure: ^(RKObjectrequestOperation * operation,NSError * error) {
    NSLog(@"FAILURE %@",error);
}];

编辑:我在RKTwitterCoreData应用程序中找到//发送请求部分之前添加了以下行,但我仍然得到相同的错误

// Other Initialization (move this to app delegatE)
[objectStore createPersistentStoreCoordinator];
[objectStore createManagedObjectContexts];

objectStore.managedObjectCache = [[RKInMemorymanagedObjectCache alloc] initWithManagedObjectContext:objectStore.persistentStoreManagedObjectContext];

解决方法

问题是映射中的路径不正确.我有http:// localhost:3000 /作为我的域名,它应该是http:// localhost:3000,我有书籍/ initial /作为它应该是/ books / initial /的路径.

RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class

我也忘了创建持久性商店.这是完整的工作示例:

// Core Data Example
// Initialize RestKIT
RKObjectManager* objectManager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:@"http://localhost:3000"]];
RKManagedObjectStore* objectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:self.model];
objectManager.managedObjectStore = objectStore;

// Mappings
RKEntitymapping *bookMapping = [RKEntitymapping mappingForEntityForName:@"Book" inManagedObjectStore:objectStore];
[bookMapping addAttributeMappingsFromArray:@[@"title"]];

RKResponseDescriptor * responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:bookMapping pathPattern:@"/books/initial/" keyPath:nil statusCodes:rKStatusCodeIndexSetForClass(RKStatusCodeClasssuccessful)];

[objectManager addResponseDescriptor:responseDescriptor];

// Other Initialization (move this to app delegatE)
[objectStore createPersistentStoreCoordinator];

NSString *storePath = [RKApplicationDataDirectory() StringByAppendingPathComponent:@"RKTwitter.sqlite"];
NSString *seedPath = [[NSBundle mainBundle] pathForresource:@"RKSeedDatabase" ofType:@"sqlite"];
NSError *error;
NSPersistentStore *persistentStore = [objectStore addsqlitePersistentStoreAtPath:storePath fromSeedDatabaseAtPath:seedPath withConfiguration:nil options:nil error:&error];
NSAssert(persistentStore,@"Failed to add persistent store with error: %@",error);

[objectStore createManagedObjectContexts];

objectStore.managedObjectCache = [[RKInMemorymanagedObjectCache alloc] initWithManagedObjectContext:objectStore.persistentStoreManagedObjectContext];

// Send request
[objectManager getObjectsAtPath:@"/books/initial/" parameters:nil success:^(RKObjectrequestOperation * operation,error);
}];

大佬总结

以上是大佬教程为你收集整理的ios – 实体(null)不是密钥值编码兼容的密钥“标题”全部内容,希望文章能够帮你解决ios – 实体(null)不是密钥值编码兼容的密钥“标题”所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。