0

我想在这里问一些事情。我已经映射了对象并将其添加到 RKResponseDescriptor 并且它可以工作,但是这里有一个问题。当来自 JSON 的数据不同时,它不会映射到已经创建的对象我只是意识到它没有很好地映射。

// Setup our object mappings
    RKObjectMapping *applicationMapping = [RKObjectMapping mappingForClass:[ApplicationSettings class]];
    [applicationMapping addAttributeMappingsFromDictionary:@{
     @"filterRead" : @"filterRead",
     @"filterUserComments" : @"filterUserComments"
     }];

    RKObjectMapping *categoryMapping = [RKObjectMapping mappingForClass:[Category class]];
    [categoryMapping addAttributeMappingsFromDictionary:@{
     @"id" : @"categoryID",
     @"title" : @"title",
     @"slug" : @"slug"
     }];

    RKObjectMapping *commentMapping = [RKObjectMapping mappingForClass:[Comment class]];
    commentMapping.forceCollectionMapping = YES;
    [commentMapping addAttributeMappingFromKeyOfRepresentationToAttribute:@"comments"];
    [categoryMapping addAttributeMappingsFromDictionary:@{
     @"(coments).id" : @"commentID",
     @"(comments).date" : @"createdDate",
     @"(comments).content" : @"comment"
     }];

    RKObjectMapping *errorMapping = [RKObjectMapping mappingForClass:[Error class]];
    [errorMapping addAttributeMappingsFromDictionary:@{
     @"error" : @"error",
     @"status" : @"status"
     }];

    RKObjectMapping *postMapping = [RKObjectMapping mappingForClass:[Post class]];
    postMapping.forceCollectionMapping = YES;
    [postMapping addAttributeMappingFromKeyOfRepresentationToAttribute:@"posts"];
    [errorMapping addAttributeMappingsFromDictionary:@{
     @"id" : @"postID",
     @"title" : @"title",
     @"content" : @"content",
     @"date" : @"createdDate",
     @"modified" : @"modifiedDate"
     }];

    RKObjectMapping *userMapping = [RKObjectMapping mappingForClass:[User class]];
    [userMapping addAttributeMappingsFromDictionary:@{
     @"nonce" : @"nonce",
     @"cookie" : @"cookie",
     @"username" : @"userName",
     @"email" : @"email"
     }];

    // Register our mappings with the provider using a response descriptor
    RKResponseDescriptor *userResponse = [RKResponseDescriptor responseDescriptorWithMapping:userMapping
                                                                                 pathPattern:nil
                                                                                     keyPath:nil
                                                                                 statusCodes:(RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful))];

    RKResponseDescriptor *applicationResponse = [RKResponseDescriptor responseDescriptorWithMapping:applicationMapping
                                                                                       pathPattern:nil
                                                                                           keyPath:nil
                                                                                       statusCodes:(RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful))];

    RKResponseDescriptor *categoryResponse = [RKResponseDescriptor responseDescriptorWithMapping:categoryMapping
                                                                                        pathPattern:nil
                                                                                            keyPath:nil
                                                                                        statusCodes:(RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful))];

    RKResponseDescriptor *commentResponse = [RKResponseDescriptor responseDescriptorWithMapping:commentMapping
                                                                                        pathPattern:nil
                                                                                            keyPath:nil
                                                                                        statusCodes:(RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful))];

    RKResponseDescriptor *errorResponse = [RKResponseDescriptor responseDescriptorWithMapping:errorMapping
                                                                                    pathPattern:nil
                                                                                        keyPath:nil
                                                                                    statusCodes:(RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful))];

    RKResponseDescriptor *postResponse = [RKResponseDescriptor responseDescriptorWithMapping:postMapping
                                                                                    pathPattern:nil
                                                                                        keyPath:nil
                                                                                    statusCodes:(RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful))];

    [objectManager addResponseDescriptorsFromArray:
     @[
        applicationResponse,
        categoryResponse,
        commentResponse,
        userResponse,
        postResponse,
        errorResponse
     ]] ;

编辑编号 1:

    [objectManager addResponseDescriptorsFromArray:
     @[
        applicationResponse,
        categoryResponse,
        commentResponse,
        errorResponse,
        postResponse,
        userResponse
     ]] ;

对于这个,可以映射用户的 JSON 数据,但不能映射另一个。我的意思是我们可以像这样映射对象吗?因为我改变了 responseDescriptors 的顺序,它只是映射了底部的对象。

对不起我以前的英语不好

4

1 回答 1

0

您的问题似乎是您没有设置pathPatternkeyPath任何描述符。这意味着无论何时 RestKit 尝试处理任何 JSON,它都会尝试处理每个响应描述符,因为它们总是匹配的。所以你会得到一些好的对象和一些空的对象(创建但没有数据集)。

您需要告诉响应描述符哪些数据适合映射。

如果您对每种类型的内容都有不同的 URL:

##_URL_##/users.json
##_URL_##/categories.json

pathPattern为每个描述符设置:

用户响应:

...ithMapping:userMapping
  pathPattern:@"users.json"
      keyPath:nil ...

如果 JSON 中有不同的内容部分,则需要使用keyPath.

将尽可能多的信息添加到pathPatternkeyPath以区分并允许 RestKit 理解您希望收到的每个请求的数据。

于 2013-04-28T08:58:59.950 回答