1

这是我第二次在这里问。我有一个类似这样的 JSON 响应

"status": "ok",
  "count": 90,
  "count_total": 44,
  "pages": 5,
  "posts": [
    {
      "id": 285,
      "type": "post",
      "slug": "lorem-ipsum",
      "url": "http:\/\/icentre.pagodabox.com\/lorem-ipsum\/",
      "status": "publish",
      "title": "Lorem Ipsum",
      "title_plain": "Lorem Ipsum",
      "content": "<p><strong>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam<\/strong>, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. <em>Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum<\/em><\/p>\n",
      "excerpt": "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat [...]",
      "date": "2013-04-29 13:11:59",
      "modified": "2013-04-29 20:57:08",
      "categories": [
        {
          "id": 9,
          "slug": "algemeen",
          "title": "Algemeen",
          "description": "Voortgang van de onderneming, visie, strategie, opinies van de directie, besluiten en grote deals.",
          "parent": 14,
          "post_count": 6
        }
      ],
      "tags": [],
      "author": {
        "id": 1,
        "slug": "admin",
        "name": "admin",
        "first_name": "",
        "last_name": "",
        "nickname": "admin",
        "url": "",
        "description": ""
      },
      "comments": [],
      "attachments": [],
      "comment_count": 0,
      "comment_status": "open"
    }

现在,我想从帖子中映射类别

在类别.h

#import <Foundation/Foundation.h>

@interface Category : NSObject

@property (copy,atomic) NSNumber *categoryID;
@property (copy,atomic) NSString *title;
@property (copy,atomic) NSString *slug;


@end

映射代码是

// Initialize RestKit
    RKObjectManager *objectManager = [[RKObjectManager alloc] initWithHTTPClient:client];
    objectManager.requestSerializationMIMEType=RKMIMETypeJSON;

    // 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 addAttributeMappingsFromDictionary:@{
     @"id" : @"commentID",
     @"date" : @"createdDate",
     @"content" : @"comment"
     }];

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

    RKObjectMapping *postMapping = [RKObjectMapping mappingForClass:[Post class]];
    [postMapping addAttributeMappingsFromDictionary:@{
     @"id" : @"postID",
     @"title" : @"title",
     @"content" : @"content",
     @"attachments" : @"attachments",
     @"author" : @"author",
     @"date" : @"createdDate",
     @"modified" : @"modifiedDate"
     }];

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

    RKObjectMapping *nonceMapping = [RKObjectMapping mappingForClass:[Session class]];
    [nonceMapping addAttributeMappingsFromDictionary:@{
     @"nonce" : @"nonce"
     }];

    // Register our mappings with the provider using a response descriptor
    RKResponseDescriptor *categoryResponse = [RKResponseDescriptor responseDescriptorWithMapping:categoryMapping
                                                                                 pathPattern:nil
                                                                                     keyPath:@"posts.categories"
                                                                                 statusCodes:(RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful))];

    RKResponseDescriptor *userResponse = [RKResponseDescriptor responseDescriptorWithMapping:userMapping
                                                                                 pathPattern:nil
                                                                                     keyPath:@"user"
                                                                                 statusCodes:(RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful))];

    RKResponseDescriptor *nonceResponse = [RKResponseDescriptor responseDescriptorWithMapping:nonceMapping
                                                                                 pathPattern:nil
                                                                                     keyPath:@"session"
                                                                                 statusCodes:(RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful))];

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


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

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

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

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

最后一件事。在日志中

2013-04-30 02:02:21.004 MyTest[15339:5a03] T restkit.object_mapping:RKMappingOperation.m:338 Found transformable value at keyPath 'id'. Transforming from type '__NSArrayI' to 'NSNumber'
2013-04-30 02:02:21.004 MyTest[15339:5a03] T restkit.object_mapping:RKMappingOperation.m:491 Skipped mapping of attribute value from keyPath 'id to keyPath 'categoryID' -- value is unchanged ((null))
2013-04-30 02:02:21.005 MyTest[15339:5a03] T restkit.object_mapping:RKMappingOperation.m:458 Mapping attribute value keyPath 'title' to 'title'
2013-04-30 02:02:21.005 MyTest[15339:5a03] T restkit.object_mapping:RKMappingOperation.m:338 Found transformable value at keyPath 'title'. Transforming from type '__NSArrayI' to 'NSString'
2013-04-30 02:02:21.005 MyTest[15339:5a03] T restkit.object_mapping:RKMappingOperation.m:491 Skipped mapping of attribute value from keyPath 'title to keyPath 'title' -- value is unchanged ((null))
2013-04-30 02:02:21.006 MyTest[15339:5a03] T restkit.object_mapping:RKMappingOperation.m:458 Mapping attribute value keyPath 'slug' to 'slug'
2013-04-30 02:02:21.006 MyTest[15339:5a03] T restkit.object_mapping:RKMappingOperation.m:338 Found transformable value at keyPath 'slug'. Transforming from type '__NSArrayI' to 'NSString'
2013-04-30 02:02:21.006 MyTest[15339:5a03] T restkit.object_mapping:RKMappingOperation.m:491 Skipped mapping of attribute value from keyPath 'slug to keyPath 'slug' -- value is unchanged ((null))
2013-04-30 02:02:21.007 MyTest[15339:5a03] D restkit.object_mapping:RKMappingOperation.m:926 Finished mapping operation successfully...
2013-04-30 02:02:21.007 MyTest[15339:5a03] D restkit.object_mapping:RKMapperOperation.m:241 Asked to map source object (

我是,在另一篇文章中搜索我必须使用 usingBlock 或其他东西。但是 usingBlock 没有显示在自动完成代码中。

4

0 回答 0