ios – 在Restkit 0.2中为给定的类添加两个请求描述符

前端之家收集整理的这篇文章主要介绍了ios – 在Restkit 0.2中为给定的类添加两个请求描述符前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要从User类中提供两种不同类型的POST.
//JSON Type A
{
    "password":"12345","email":"test@gmail.com"
}

//JSON Type B
{
   "user":{
      "Password":"12345","Email":"sample@gmail.com"
   }
}

我试图制作两个请求描述符并将其添加到我的对象管理器,但是我收到错误

“Cannot add a request descriptor for the same object class as an
existing request descriptor.”

我的代码

@interface User : NSObject

@property (nonatomic,retain) NSString * userID;
@property (nonatomic,retain) NSString * email;
@property (nonatomic,retain) NSString * password;
@property (nonatomic,retain) NSString * firstName;
@property (nonatomic,retain) NSString * lastName;

@end

- (void)setupUserMapping:(RKObjectManager *)objectManager {

    // Setup user response mappings
    RKObjectMapping *userMapping = [RKObjectMapping mappingForClass:[User class]];
    [userMapping addAttributeMappingsFromDictionary:@{
     @"ID" :@"userID",@"Email" : @"email",@"Password" : @"password",@"FirstName" : @"firstName",@"LastName" : @"lastName",}];


    RKResponseDescriptor *responseDescriptorAuthenticate = [RKResponseDescriptor responseDescriptorWithMapping:userMapping
                                                                                       pathPattern:@"/Authenticate"
                                                                                           keyPath:nil
                                                                                       statusCodes:[NSIndexSet indexSetWithIndex:200]];


    RKResponseDescriptor *responseDescriptorRegister = [RKResponseDescriptor responseDescriptorWithMapping:userMapping
                                                                                                   pathPattern:@"/Register"
                                                                                                       keyPath:nil
                                                                                                   statusCodes:[NSIndexSet indexSetWithIndex:200]];
    [objectManager addResponseDescriptor:responseDescriptorRegister];
    [objectManager addResponseDescriptor:responseDescriptorAuthenticate];

    // Setup user request mappings
    RKObjectMapping* userRequestMappingForRegister = [RKObjectMapping requestMapping];
    [userRequestMappingForRegister addAttributeMappingsFromDictionary:@{
     @"email" : @"Email",@"password" : @"Password",@"firstName" : @"FirstName",@"lastName" : @"LastName",}];
    RKRequestDescriptor *requestDescriptorForRegister = [RKRequestDescriptor requestDescriptorWithMapping:userRequestMappingForRegister objectClass:[User class] rootKeyPath:@"user"];


    RKObjectMapping* userRequestMappingForAuthenticate = [RKObjectMapping requestMapping];
    [userRequestMappingForAuthenticate addAttributeMappingsFromDictionary:@{
     @"userID" :@"ID",@"email" : @"email",@"password": @"password"
     }];
    RKRequestDescriptor *requestDescriptorForAuthenticate = [RKRequestDescriptor requestDescriptorWithMapping:userRequestMappingForAuthenticate objectClass:[User class] rootKeyPath:nil];

    [objectManager addRequestDescriptor:requestDescriptorForRegister];
    [objectManager addRequestDescriptor:requestDescriptorForAuthenticate];
}

有没有人知道如何解决这个问题,而不需要为这些请求创建一个单独的类?

任何帮助是赞赏.

谢谢.

解决方法

您可以使用动态映射来切换序列化行为.如果这是一个很常见的问题,我们可以想象地将路径匹配添加到请求描述符中.我只是没有这么多的要求.

有一个例子说明如何在单元测试中使用带有请求的动态映射:https://github.com/RestKit/RestKit/blob/master/Tests/Logic/ObjectMapping/RKObjectParameterizationTest.m#L495-L534

猜你在找的iOS相关文章