使用Restkit 0.20发布字典数组

时间:2013-08-19 23:29:22

标签: objective-c json nsdictionary restkit

我无法正常运行。

我已经映射了我想要注入请求的JSON有效负载的键值对,但最终每个帖子都失败了。

以下几行显示我创建了要求:

RKObjectMapping *requestMapping = [WDMappingProvider createRegInitRequestTicketMapping];
    RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:[requestMapping inverseMapping] objectClass:[WDTicketsModel class] rootKeyPath:nil];
[registrationManager addRequestDescriptor:requestDescriptor];

然后我创建了发布的路径并将数据输入到我的帖子请求中:

//Make object from params
    WDRegistrationInitializeRequestModel *requestModel  = [[WDRegistrationInitializeRequestModel alloc] init];

    requestModel.eventID = [params objectForKey:@"eventID"];
    requestModel.arrayOfTicketDictionaries  = [params objectForKey:@"arrayOfTicketDictionaries"];

    NSString *postPath = [NSString stringWithFormat:@"api/v1/registrations/initialize?eventid=%@", requestModel.eventID];

    //Post the object (aka the interest json payload)
    [registrationManager postObject:requestModel.arrayOfTicketDictionaries path:postPath parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *result){ ...

此时我总是在postObject方法的Failure块中结束。

[WDMappingProvider createRegInitRequestTicketMapping]的映射如下所示:

+(RKObjectMapping *)createRegInitRequestTicketMapping
{
    RKObjectMapping *regInitRequestMapping = [RKObjectMapping mappingForClass:[WDTicketsModel class]];
    [regInitRequestMapping addAttributeMappingsFromDictionary:
     @{
        @"Quantity" : @"quantity",
        @"TicketID":@"mainID"
     }];


    return regInitRequestMapping;
}

arrayOfTicketDictionaries看起来像这样:

[{"Quantity":1,"TicketID":1604},{"Quantity":1,"TicketID":1605},{"Quantity":1,"TicketID":1606}]

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我认为这是因为你错过了RKResponseDescriptor部分。尝试添加这个:

// Anything in 2xx
NSIndexSet *statusCodes =
  RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful);
RKResponseDescriptor *responseDescriptor =
  [RKResponseDescriptor
   responseDescriptorWithMapping:requestMapping
   method:RKRequestMethodAny
   pathPattern:nil
   keyPath:nil
   statusCodes:statusCodes];
[registrationManager addResponseDescriptor: responseDescriptor];