使用Restkit进行映射,该映射具有关系并且没有数据的某些部分的关键路径 [英] Mapping with Restkit with relationships and no key path for some part of the data

查看:94
本文介绍了使用Restkit进行映射,该映射具有关系并且没有数据的某些部分的关键路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试映射此JSON数据:

I am trying to map this JSON data:

{
  "Id": 1,
  "Question": [
    {
      "Id": 1,
      "PicUrl": "sample string 2",
      "CorrectAnswer": "sample string 3",
      "Difficulty": 4,
      "CategoryId": 5,
      "CategoryName": "sample string 6",
      "AccountId": 7
    },
    {

      "CorrectAnswer": "sample string 3",
      "Difficulty": 4,
      "CategoryId": 5,
      "CategoryName": "sample string 6",
      "AccountId": 7
    },
    {
  "StartTime": "2013-10-09T00:54:46.5522592+00:00"
}

但是我有一些问题.反复浏览 Restkit的对象映射概述后,我设置了将我的数据分为3个不同的类别. CurrentGames,项目和答案.我将答案类映射为没有键路径,如

but I am having some problems. After going trough the object mapping overview of Restkit over and over again,I set my divided my data to 3 different classes. CurrentGames,items and answers. I mapped the answer class as no key-path as described in here and for the items class I created a relation to answers class. And i created an another relation from items to currentgames class. I believe I did the mapping part right. But I am missing something in my code because I am not getting the data properly. I've been trying to configure this out for a week now. If someone can help, I would appreciate big time!

这是我的控制台的样子:

2013-10-08 20:24:38.366 FlickRest[5126:a0b] I restkit:RKLog.m:34 RestKit logging initialized...
2013-10-08 20:24:38.518 FlickRest[5126:a0b] answers mapping <RKObjectMapping:0x8e59110 objectClass=answers propertyMappings=(
    "<RKAttributeMapping: 0x8e576f0 (null) => answerss>"
)> 
2013-10-08 20:24:38.519 FlickRest[5126:a0b] items mapping <RKObjectMapping:0x8e57aa0 objectClass=items propertyMappings=(
    "<RKAttributeMapping: 0x8e30cd0 CategoryId => CategoryId>",
    "<RKAttributeMapping: 0x8e56560 AccountId => accountId>",
    "<RKAttributeMapping: 0x8e56580 Id => ID>",
    "<RKAttributeMapping: 0x8e552a0 CategoryName => CategoryName>",
    "<RKAttributeMapping: 0x8e552e0 Difficulty => Difficulty>",
    "<RKAttributeMapping: 0x8e587c0 CorrectAnswer => correctAnswer>",
    "<RKAttributeMapping: 0x8e55400 PicUrl => PicURL>",
    "<RKRelationshipMapping: 0x8e567c0 Answers => Answers>"
)> 
2013-10-08 20:24:38.520 FlickRest[5126:a0b] currentGames mapping <RKObjectMapping:0x8e569a0 objectClass=CurrentGames propertyMappings=(
    "<RKAttributeMapping: 0x8e56a00 StartTime => StartTime>",
    "<RKAttributeMapping: 0x8e56a20 GameId => GameId>",
    "<RKRelationshipMapping: 0x8e595b0 Items => Items>"
)> 
2013-10-08 20:24:38.549 FlickRest[5126:a0b] I restkit.network:RKObjectRequestOperation.m:180 GET 'https://xxx.net'
2013-10-08 20:24:39.287 FlickRest[5126:3507] I restkit.network:RKObjectRequestOperation.m:250 GET 'xxx.net' (200 OK / 1 objects) [request=0.7201s mapping=0.0172s total=0.7687s]
2013-10-08 20:24:39.287 FlickRest[5126:a0b] I app:WelcomeViewController.m:91 Load collection of Articles: (
    "<CurrentGames: 0x8b90d70>"
)

我的课程:

answers.h

answers.h

#import <Foundation/Foundation.h>

@interface answers : NSObject

@property (nonatomic,copy) NSString * answerss;
@end

items.h

#import <Foundation/Foundation.h>
#import "answers.h"

@interface items : NSObject

@property (nonatomic)NSNumber *ID;
@property(nonatomic,copy)NSURL *PicURL;
@property (nonatomic,copy)NSString *correctAnswer;
@property (nonatomic)NSNumber *Difficulty;
@property(nonatomic) NSNumber *CategoryId;
@property(nonatomic,copy)NSString *CategoryName;
@property(nonatomic)NSNumber *accountId;
@end

CurrentGames.h

CurrentGames.h

#import <Foundation/Foundation.h>
#import "items.h"
@interface CurrentGames : NSObject

@property (nonatomic,copy)NSNumber *GameId;
@property (nonatomic) items *Items;
@property (nonatomic,copy) NSDate *StartTime;
@end

我正在使用的视图控制器:

WelcomeViewController.m

WelcomeViewController.m

@implementation WelcomeViewController

-(void)loadGame
{
    // Mapping for the answers class. Mapped as an array with no key-path

    RKObjectMapping *answersMapping = [RKObjectMapping mappingForClass:[answers class]];
    [answersMapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:nil toKeyPath:@"answerss"]];

    // Mapping for the items class. Mapped as relation to  answers class

    RKObjectMapping *itemsMapping =[RKObjectMapping mappingForClass:[items class]];
    [itemsMapping addAttributeMappingsFromDictionary:@{@"Id":@"ID",
                                                       @"PicUrl":@"PicURL",
                                                       @"CorrectAnswer":@"correctAnswer",
                                                       @"Difficulty":@"Difficulty",
                                                       @"CategoryId":@"CategoryId",
                                                       @"CategoryName":@"CategoryName",
                                                       @"AccountId":@"accountId"
                                                       }];
    [itemsMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"Answers" toKeyPath:@"Answers" withMapping:answersMapping]];

    //Mapping for the CurrentGames class. Mapped as relation to items class.

    RKObjectMapping *CurrentGamesMapping = [RKObjectMapping mappingForClass:[CurrentGames class]];
    [CurrentGamesMapping addAttributeMappingsFromDictionary:@{@"GameId":@"GameId",
                                                              @"StartTime":@"StartTime"}];

    [CurrentGamesMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"Items" toKeyPath:@"Items" withMapping:itemsMapping]];

    // Response descriptor for all classes
    RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:CurrentGamesMapping method:RKRequestMethodGET pathPattern:nil keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

    // Requesting data with URL

    NSURL *URL = [NSURL URLWithString:@"xxx.net"];
    NSURLRequest *request = [NSURLRequest requestWithURL:URL];

    RKObjectRequestOperation *objectRequestOperation =[[RKObjectRequestOperation alloc]initWithRequest:request responseDescriptors:@[responseDescriptor]];

    [objectRequestOperation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
        RKLogInfo(@"Load collection of Articles: %@", mappingResult.array);
    } failure:^(RKObjectRequestOperation *operation, NSError *error) {
        RKLogError(@"Operation failed with error: %@", error);
    }];

    [objectRequestOperation start];
    NSLog(@"answers mapping %@ ",answersMapping);
    NSLog(@"items mapping %@ ",itemsMapping);
    NSLog(@"currentGames mapping %@ ",CurrentGamesMapping);
}

推荐答案

此属性:

@property (nonatomic) answers *Answers;

应为:

@property (strong, nonatomic) NSArray *Answers;

因为您有一个答案对象列表.

because you have a list of answers objects.

此属性:

@property (nonatomic) items *Items;

有同样的问题.这两个错误将阻止您的映射按预期工作.

has the same issue. These 2 errors will prevent your mappings from working as you expect.

这篇关于使用Restkit进行映射,该映射具有关系并且没有数据的某些部分的关键路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆