0

我在 Restkit 0.20 中遇到了请求映射问题。我想将一个带有 NSStrings 的 NSArray 放入键“mails”下的请求中,例如:

{mails:[@"first@gmail.com",@"second@gmail.com"]}

因此,对于这种情况,我真的不需要对象映射,因为我只使用标准对象。我只是没有让它工作,所以我切换回常规方式(至少对我来说) - 引入一个包含 NSArray 的 DTO 对象 MailRequest。我这样做:

RKObjectMapping* mapping =  [RKObjectMapping requestMapping];
[mapping addAttributeMappingsFromDictionary:@{
        @"mails":@"mails"
}];  

RKRequestDescriptor *reqDesc = 
            [RKRequestDescriptor requestDescriptorWithMapping:mapping
                                                  objectClass:[MailRequest class]
                                                  rootKeyPath:nil];

RKObjectManager *manager = ...
...
NSMutableURLRequest *request = [manager requestWithObject:requestObject 
                                                   method:RKRequestMethodPOST 
                                                     path:urlString parameters:nil];

RKObjectRequestOperation *operation = 
                     [manager objectRequestOperationWithRequest:request ...

...但我想摆脱 MailsRequest DTO 对象。那可能吗?

4

1 回答 1

2

跳过映射步骤,只需使用 RestKit 进行发送和任何响应映射。以您想要的任何方式构建您的字典。您确实需要从urlString.

NSDictionary *mails = @{mails:@[@"first@gmail.com",@"second@gmail.com"]};
NSError *error = nil;
NSData *mailsJSON = [NSJSONSerialization dataWithJSONObject:mails options:0 error:&error];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:mailsJSON];

RKObjectRequestOperation *operation = [manager objectRequestOperationWithRequest:request ...
于 2013-05-24T16:18:49.503 回答