我正在尝试使用 RESTKit 发布一些数据(具体来说是网站的身份验证令牌),但遇到了麻烦。每次我运行该方法时,我都会得到:
'NSInvalidArgumentException', reason: '`RKRequestDescriptor` objects must be initialized
with a mapping whose target class is `NSMutableDictionary`, got 'Login' (see
`[RKObjectMapping requestMapping]`)'
此外,代码中有许多“不兼容的指针类型”警告。
这是我的方法,它非常丑陋和糟糕,我需要一些帮助来解决。[self postToken]
当按下按钮时,我在 IBAction 方法中执行一个操作。我从另一个文件中获取变量 userAuthToken,该文件是使用来自 POST 请求的 json 响应设置的,而不是使用 RESTKit。当我知道如何成功时,我最终会将该 POST 转换为 RESTKit。“userAuthTokenMethod”方法与“postToken”方法在同一个文件中,它允许我使用在 AppDelegate 中初始化的 userAuthToken 对象,因此它充当全局变量。
- (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
这是初始化“令牌”的登录文件的代码:
//Login.h
#import <Foundation/Foundation.h>
@interface Login : NSObject
@property (nonatomic, copy)NSString *token;
@end
我对 RESTKit 很陌生,对 Objective-C 也有些陌生(我已经在里面编码了大约一个月)。我将不胜感激修复我的代码的所有帮助。