1

我发现了我的错误,所以请滚动到更新以查看我做错了什么以及我在使用 RestKit 时遇到的其他问题 :)

我对以下 JSON 有疑问:

[ {“id”:1,“latTitle”:“Aale”,“latExamplesString”:“”,“previewImg”:1,“lastModified”:null,“localeCategory”:{“id”:1,“catID”: 1,“langCode”:“de”,“localeTitle”:“Aale”,“localeExampleString”:“Gefleckter Schlangenaal,Ringelschlangenaal”},“elements”:null}]

如您所见,localeCategory 是一个嵌套的属性列表,因此我为映射它做了以下操作:

        //creating Mapping for localeCategory
    RKManagedObjectMapping *localeMapping = [RKManagedObjectMapping mappingForClass:[LocaleCategory class] inManagedObjectStore:objectStore];
    localeMapping.primaryKeyAttribute = @"lcatId";
    [localeMapping mapKeyPath:@"id" toAttribute:@"lcatId"];
    [localeMapping mapKeyPath:@"catId" toAttribute:@"catId"];
    [localeMapping mapKeyPath:@"langCode" toAttribute:@"langCode"];
    [localeMapping mapKeyPath:@"localeTitle" toAttribute:@"localeTitle"];
    [localeMapping mapKeyPath:@"localeExampleString" toAttribute:@"localeExampleString"];

    //creating Mapping for Category
    RKManagedObjectMapping *categoryMapping = [RKManagedObjectMapping mappingForClass:[Category class] inManagedObjectStore:objectStore];
    categoryMapping.primaryKeyAttribute = @"catId";
    [categoryMapping mapKeyPath:@"id" toAttribute:@"catId"];
    [categoryMapping mapKeyPath:@"latTitle" toAttribute:@"latTitle"];
    [categoryMapping mapKeyPath:@"latExamplesString" toAttribute:@"latExamplesString"];
    [categoryMapping mapKeyPath:@"previewImg" toAttribute:@"previewImg"];
    [categoryMapping mapKeyPath:@"lastModified" toAttribute:@"lastModified"];
    [categoryMapping mapKeyPath:@"elements" toAttribute:@"elements"];

    [categoryMapping mapKeyPath:@"localeCategory" toRelationship:@"localeCategory" withMapping:localeMapping];

    //[categoryMapping mapRelationship:@"localeCategory" withMapping:localeMapping];

    [self setObjectMapping:categoryMapping forResourcePathPattern:@"data/de/category"];

我的问题如下:

当我想将JSON文件中的数据列出到我的TableView中时,localeCategory的所有属性都是空的,例如我想设置一个单元格的标题:

Category *cat = [data objectAtIndex:absoluteRow];
    LocaleCategory *loccat = [cat localeCategory];
    NSLog(@"gotCategory, check localeCat: %@", loccat.localeTitle);
cell.catTitle.text = cat.localeCategory.localeTitle;

我已经查看了 RestKit 的每个 SampleCode 以及堆栈溢出的相关主题,但我找不到我的错误!

预先感谢您的每一个帮助:)

干杯,麦克斯

更新:我在使用 FetchRequest 时也遇到了麻烦:

NSLog(@"fetchDataFromDataStore");
NSFetchRequest *request = [[[RKObjectManager sharedManager] mappingProvider] fetchRequestForResourcePath:self.resourcePath];
NSLog(@"request");
self.data = [Category objectsWithFetchRequest:request];

最终出现此错误:

'executeFetchRequest:error: 获取请求必须有一个实体。

我还想知道的是我不能使用这个:

Category *firstCategory = [Category objectWithPrimaryKeyValue:[NSNumber numberWithInt:1]];

它说不知道选择器的类方法...

我希望有人能帮助我找出我的错误,看起来我在使用 Reskit 和 CoreData 进行设置的一般过程中犯了一个错误!

我期待您的帮助,非常感谢!

谢谢rdelmar!我已经修复了它,但我认为这不是问题。我第一次使用没有 CoreData 的 RestKit,所以我在 LocaleCategory 类中使​​用了@synthesize 而不是 @dynamic :) 我所有其他类都已经切换到动态,所以是的,这是一个愚蠢的错误!:)

无论如何,我仍然有 FetchRequest 的问题!如果有人有想法,我非常感谢!

最大限度

4

1 回答 1

0

好的,我现在可以使用 RestKit 了 :)

这是我的问题的解决方案:

“executeFetchRequest:错误:获取请求必须有一个实体。”

        [self setObjectMapping:categoryMapping forResourcePathPattern:@"category" withFetchRequestBlock:^NSFetchRequest *(NSString *resourcePath) {
        NSFetchRequest *fetchRequest = [Category fetchRequest];
        fetchRequest.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"latTitle" ascending:YES]];
        return fetchRequest;
    }];

和电话:

 NSFetchRequest *request = [[[RKObjectManager sharedManager] mappingProvider] fetchRequestForResourcePath:@"category"];
self.data = [Category objectsWithFetchRequest:request];

当您知道如何时,这很容易:)

于 2012-12-12T13:25:22.573 回答