0

我正在使用我自己的 JSON 数据关注 restkit 的概述教程

如教程中所述,我使用的是主表视图样板。根据我的实现,我能够在控制台中显示映射,但我无法在我的表视图单元格中将正确答案显示为文本标签。我很感激,如果有人可以向我解释为什么我没有得到单元格标签中的文本。

这是我为单元格标签文本指定的地方

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
    NSManagedObject *object = [self.fetchedResultsController objectAtIndexPath:indexPath];
    cell.textLabel.text = [[object valueForKey:@"CorrectAnswer"] description];
    NSLog(@"This is what i am looking for: %@", [object valueForKey:@"CorrectAnswer"]);

}

这是我的控制台结果

2013-10-16 05:07:31.963 Quizi[27422:a0b] I restkit:RKLog.m:34 RestKit logging initialized...
2013-10-16 05:07:32.281 Quizi[27422:a0b] T restkit.network:RKObjectRequestOperation.m:178 GET 'https://xxx.xxx./':
request.headers=(null)
request.body=(null)
2013-10-16 05:07:32.777 Quizi[27422:1003] T restkit.network:RKResponseMapperOperation.m:451 Mapping HTTP response to nil target object...
2013-10-16 05:07:32.781 Quizi[27422:1003] W restkit.core_data:RKManagedObjectMappingOperationDataSource.m:243 Performing managed object mapping with a nil managed object cache:
Unable to update existing object instances by identification attributes. Duplicate objects may be created.
2013-10-16 05:07:32.783 Quizi[27422:1003] W restkit.object_mapping:RKMapperOperation.m:99 Adding mapping error: No mappable values found for any of the attributes or relationship mappings
2013-10-16 05:07:32.788 Quizi[27422:3407] T restkit.network:RKObjectRequestOperation.m:248 GET 'https://xxx.xxx/22' (200 OK / 0 objects) [request=0.4945s mapping=0.0117s total=0.5176s]:
response.headers={
    "Cache-Control" = "max-age=100";
    "Content-Encoding" = gzip;
    "Content-Type" = "application/json; charset=utf-8";
    "DWAS-Handler-Name" = "System.Web.Http.WebHost.HttpControllerHandler";
    Date = "Wed, 16 Oct 2013 12:07:34 GMT";
    Etag = "\"dbc1a97b-e968-4a2a-a697-77328a8d5b94\"";
    Server = "Microsoft-IIS/8.0";
    "Set-Cookie" = "ARRAffinity=ba0ce9e0d0d8ad0cd8b963604bd90760f5428ad8490fed51bd3786d0fe9002e3;Path=/;Domain=somedomain, WAWebSiteSID=a474e52a3f1f49f48da74b52968824b9; Path=/; HttpOnly";
    "Transfer-Encoding" = Identity;
    Vary = "Accept-Encoding";
    "X-AspNet-Version" = "4.0.30319";
    "X-Powered-By" = "ASP.NET, ARR/2.5, ASP.NET";
}
response.body={"some response body here"}

这是我在主表视图中的获取结果控制器

- (NSFetchedResultsController *)fetchedResultsController
{
    if (_fetchedResultsController != nil) {
        return _fetchedResultsController;
    }

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    // Edit the entity name as appropriate.
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Items" inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entity];

    // Set the batch size to a suitable number.
    [fetchRequest setFetchBatchSize:20];

    // Edit the sort key as appropriate.
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"CorrectAnswer" ascending:NO];
    NSArray *sortDescriptors = @[sortDescriptor];
    [fetchRequest setSortDescriptors:sortDescriptors];

    // Edit the section name key path and cache name if appropriate.
    // nil for section name key path means "no sections".
    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@"Master"];
    aFetchedResultsController.delegate = self;
    self.fetchedResultsController = aFetchedResultsController;

    NSError *error = nil;
    if (![self.fetchedResultsController performFetch:&error]) {
         // Replace this implementation with code to handle the error appropriately.
         // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. 
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }

    return _fetchedResultsController;
}    

这就是我映射的方式

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.navigationItem.leftBarButtonItem = self.editButtonItem;

    UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject:)];
    self.navigationItem.rightBarButtonItem = addButton;
      RKManagedObjectStore *managedObjectStore = [RKManagedObjectStore defaultStore];
    RKEntityMapping *entityMapping = [RKEntityMapping mappingForEntityForName:@"Items" inManagedObjectStore:managedObjectStore];
    [entityMapping addAttributeMappingsFromDictionary:@{@"Id":@"iD",
                                                        @"PicUrl":@"picURL",
                                                        @"CorrectAnswer":@"correctAnswer",
                                                        @"Difficulty":@"difficulty",
                                                        @"CategoryId":@"categoryID",
                                                        @"CategoryName":@"categoryName",
                                                        @"AccountId":@"accountID"
                                                        }];
    RKLogConfigureByName("RestKit/Network", RKLogLevelTrace);

    RKResponseDescriptor *responseDescriptor =[RKResponseDescriptor responseDescriptorWithMapping:entityMapping method:RKRequestMethodGET pathPattern:nil keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https:someDomain"]];
    RKManagedObjectRequestOperation *managedObjectRequestOperation =[[RKManagedObjectRequestOperation alloc]initWithRequest:request responseDescriptors:@[responseDescriptor]];
    managedObjectRequestOperation.managedObjectContext = self.managedObjectContext;
    [[NSOperationQueue currentQueue]addOperation:managedObjectRequestOperation];

}
4

1 回答 1

0

这行日志告诉你 RestKit 不喜欢你的设置:

2013-10-16 05:07:32.783 Quizi[27422:1003] W restkit.object_mapping:RKMapperOperation.m:99 Adding mapping error: No mappable values found for any of the attributes or relationship mappings

问题是您需要将响应描述符的键路径设置为@"Items"并且您实际上已将其设置为nil(因此 RestKit 无法找到要映射的正确数据)。

于 2013-10-16T12:59:00.743 回答