我正在尝试使用RESTKit发布一些数据(一个网站的身份验证令牌,具体而言),我遇到了麻烦。每次运行该方法时,我都会得到:
'NSInvalidArgumentException', reason: '`RKRequestDescriptor` objects must be initialized
with a mapping whose target class is `NSMutableDictionary`, got 'Login' (see
`[RKObjectMapping requestMapping]`)'
此外,代码中还有许多“不兼容的指针类型”警告。
这是我的方法真的很丑陋和坏,我想帮助修复。按下按钮时,我在IBAction方法中执行[self postToken]
。我从另一个文件获取变量userAuthToken,使用来自不使用RESTKit的POST请求的json响应来设置它。当我知道如何成功时,我最终将该POST转换为RESTKit。方法“userAuthTokenMethod”与“postToken”方法位于同一文件中,它允许我使用userAuthToken对象,该对象在AppDelegate中初始化,因此它充当全局变量。
- (void)postToken
{
RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[Login class]];
[mapping addAttributeMappingsFromDictionary:@{@"token": @"token"}];
NSIndexSet *statusCodeSet = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful);
RKResponseDescriptor *tokenResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:mapping method:nil pathPattern:nil keyPath:nil statusCodes:statusCodeSet];
RKRequestDescriptor *tokenRequestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:mapping objectClass:self rootKeyPath:nil method:nil];
[[RKObjectManager sharedManager] addRequestDescriptor:tokenRequestDescriptor];
RKObjectManager *tokenManager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:@"https://websitetest.com/doctors/find"]];
[tokenManager addResponseDescriptor:tokenResponseDescriptor];
[tokenManager addRequestDescriptor:tokenRequestDescriptor];
[tokenManager postObject:[[self userAuthTokenMethod] userAuthToken] path:nil parameters:nil success:nil failure:nil];
NSURL *tokenURL = [NSString stringWithFormat:@"https://websitetest.com/doctors/find?name=%@&location=%@",nameIDTextField.text, locationTextField.text];
NSMutableURLRequest *tokenRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:tokenURL]];
RKObjectManager *tokenObjectManager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:tokenURL]];
[tokenObjectManager.HTTPClient setDefaultHeader:@"Auth-Token" value:[[self network360Edge] userAuthToken]];
RKObjectRequestOperation *tokenOperation = [[RKObjectRequestOperation alloc] initWithRequest:tokenRequest responseDescriptors:@[tokenResponseDescriptor]];
NSLog(@"Token being POSTed ==> %@", [[self userAuthTokenMethod] userAuthToken]);
[tokenOperation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *result){
NSLog(@"Response for POST request with token ==> %@", [result array]);
}failure:nil];
[tokenOperation start];
}
以下是名为MappingProvider的映射提供程序文件的代码:
//MappingProvider.h
#import <Foundation/Foundation.h>
#import <RestKit/RestKit.h>
@interface MappingProvider : NSObject
+(RKMapping *)tokenMapping;
@end
//MappingProvider.m
#import "MappingProvider.h"
#import <RestKit/RestKit.h>
#import "Login.h"
@implementation MappingProvider
+(RKMapping *)tokenMapping
{
RKObjectMapping *tokenMapping = [RKObjectMapping mappingForClass:[Login class]];
[tokenMapping addAttributeMappingsFromArray:@[@"token"]];
return tokenMapping;
}
@end
以下是登录文件的代码,其中'token'已初始化:
//Login.h
#import <Foundation/Foundation.h>
@interface Login : NSObject
@property (nonatomic, copy)NSString *token;
@end
我是RESTKit的新手,而且是Objective-C的新手(我已经在其中编写了大约一个月)。我很感激修复代码的所有帮助。
答案 0 :(得分:0)
这个问题很老,但问题是请求应该使用没有类规范的映射,因为请求使用指定类的描述符(参见下面的2行),如下所示:
RKObjectMapping *requestMapping = [RKObjectMapping requestMapping];
可能你也有问题:
RKRequestDescriptor *tokenRequestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:mapping objectClass:self rootKeyPath:nil method:nil];
更具体地说,在 objectClass:self 中,它应该是 objectClass:[self class] 或包含 token 属性的所需类,例如这样:
RKRequestDescriptor *tokenRequestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:requestMapping objectClass:[self class] rootKeyPath:nil method:nil];
希望这有帮助