如何将简单字典映射到restkit中的简单类

时间:2013-06-26 03:52:59

标签: ios restkit

我很难在restkit中使用基本的东西。我也是一位经验丰富的开发人员,这使得它更令人沮丧。

我已成功将json对象发布到我的服务,并通过RestKit源调试我已验证已返回所需的json。它返回到成功块,但除了空对象外什么都没有。

这是我的代码,用于设置映射并启动请求。

注意:我隐藏了一些敏感的密码/网址。

RKObjectMapping *requestMapping = [RKObjectMapping requestMapping];
[requestMapping addAttributeMappingsFromDictionary:@{ @"DeviceKey": @"DeviceKey", @"DeviceName": @"DeviceName", @"Password": @"Password", @"Username": @"Username"}];

RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:requestMapping
                                                                               objectClass:[LoginRequest class]
                                                                               rootKeyPath:nil];


RKObjectMapping *responseMapping = [RKObjectMapping mappingForClass:[LoginResponse class]];
responseMapping.forceCollectionMapping = YES;
[responseMapping addAttributeMappingsFromDictionary:@{@"TotalBalance":@"TotalBalance",@"FirstName":@"FirstName"}];

RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:responseMapping
                                                                                   pathPattern:@"Login"
                                                                                       keyPath:nil
                                                                                   statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

RKObjectManager *manager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:@"http://api.test.com/account/"]];
[manager addRequestDescriptor:requestDescriptor];
[manager addResponseDescriptor:responseDescriptor];
manager.requestSerializationMIMEType = RKMIMETypeJSON;

LoginRequest *request = [[LoginRequest alloc] init];
request.DeviceKey = @"xxxxxxx";
request.DeviceName = @"iPhone";
request.Username = @"mike";
request.Password = @"******";

[manager postObject:request path:@"Login" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *result) {
    NSArray * array = [result array];
    NSDictionary * dictionary = [result dictionary];
    NSObject * obj = [result firstObject];
    NSLog(@"We object mapped the response with the following result: %@", result);
    // It ends up in the success callback but all of these are EMPTY :(
} failure:^(RKObjectRequestOperation *operation, NSError *err){
    NSLog(@"We had a problemo");
}];

LogonResponse.h(.m只是合成它们的属性)

#import <Foundation/Foundation.h>

@interface LoginResponse : NSObject

@property(nonatomic,assign) double TotalBalance;
@property(nonatomic,retain) NSString * FirstName;

@end

JSON:

{
    "TotalBalance":22.34,
    "FirstName":"Mike"
    ........ other stuff but this shouldn't matter
}

控制台显示:

2013-06-26 14:03:07.173 Tests.com[1666:907] I restkit.support:RKMIMETypeSerialization.m:115 JSON Serialization class 'RKNSJSONSerialization' detected: Registering for MIME Type 'application/json
2013-06-26 14:03:07.181 Tests.com[1666:3907] T restkit.network:RKHTTPRequestOperation.m:139 POST 'http://api.test.com/account/Login':
request.headers={
    Accept = "application/json";
    "Accept-Language" = "en-GB, en, fr, de, ja, nl, it, es, pt, pt-PT, da, fi, nb, sv, ko, zh-Hans, zh-Hant, ru, pl, tr, uk, ar, hr, cs, el, he, ro, sk, th, id, ms, ca, hu, vi, en-us;q=0.8";
    "Content-Type" = "application/json; charset=utf-8";
    "User-Agent" = "Tests.com/2.10.31_dev (iPad; iOS 6.1.3; Scale/1.00)";
}
request.body={"DeviceKey":"xxxxxxx","DeviceName":"iPhone","Password":"******","Username":"mike"}
2013-06-26 14:03:07.297 Tests.com[1666:162f] D restkit.network:RKHTTPRequestOperation.m:191 Received authentication challenge
2013-06-26 14:03:08.016 Tests.com[1666:162f] T restkit.network:RKHTTPRequestOperation.m:156 POST 'http://api.test.com/account/Login' (200):
response.headers={
    "Content-Length" = 1172;
    "Content-Type" = "application/json; charset=utf-8";
    Date = "Wed, 26 Jun 2013 04:03:04 GMT";
    Server = "Microsoft-IIS/7.5";
    Vary = "Accept-Encoding";
    "X-Powered-By" = "ASP.NET";
}
response.body={"CustomerReference":"50713881","FirstName":"Mike","TotalBalance":22.25,"ErrorInfo":null,"Success":true}
2013-06-26 14:03:08.021 Tests.com[1666:3b13] W restkit.object_mapping:RKMapperOperation.m:76 Adding mapping error: Cannot map a collection of objects onto a non-mutable collection. Unexpected destination object type 'LoginRequest'
(lldb) 

为什么会继续关于LoginRequest?我迷路了!!

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:0)

当您使用RestKit发布请求时,我相信映射器会尝试将响应映射到发送的相同对象类型。在这种情况下,因为发送了LoginRequest,所以它希望将响应映射到LoginRequest对象。

对此的解决方案可能是创建一个临时的LoginResponse对象,并使用包含您的登录请求字段(用户名,密码等)的字典发布该对象。这样,RestKit就能将响应映射到您创建的临时对象中。

答案 1 :(得分:0)

升级到v0.20.3解决了这个问题。