我的代码如下:
NSString *urlString = [[NSString stringWithFormat:@"/players?skip=%ld",(long)skipSize] DVURL];
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
//[request set]
[request setValuesForKeysWithDictionary:[filter filteringDictionary]];
AFHTTPRequestOperation *operation =
[[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.responseSerializer = [AFJSONResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:
^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"responseObject %@", responseObject);
totalPages = ([[responseObject
objectForKey:@"count"] intValue] / 20);
NSLog(@"%@",[responseObject objectForKey:@"players"]);
for (NSDictionary *playerDict in [responseObject objectForKey:@"players"]) {
DVPlayer *player = [[DVPlayer alloc] initWithDictionary:playerDict];
if (![self.playerArray.players containsObject:player]) {
[self.playerArray.players addObject:player];
}
}
[playerCollectionView reloadData];
} failure:
^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", [error localizedDescription]);
[[[UIAlertView alloc]
initWithTitle:@"Error fetching players!"
message:@"Please try again later"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil] show];
}];
[operation start];
当我使用AFHTTPRequestOperationManager
时,我使用NSDictionary
作为GET方法的参数。
所以我认为[request setValuesForKeysWithDictionary:[filter filteringDictionary]];
会对应。
但我收到了一个错误:
setValue:forUndefinedKey:]:这个类是 不符合名称的密钥值编码(ex)
为方便起见,我将参数设置为NSDictionary
...
使用NSDictionary
时,我不能将参数设置为AFHTTPRequestOperation
,而不是AFHTTPRequestOperationManager
吗?
请帮帮我
答案 0 :(得分:0)
AFHTTPRequestOperation
将NSURLRequest
作为init参数。因此,您需要自己处理序列化,将其附加到您的网址,然后对网址进行编码:
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[yourUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
另一个原因AFHTTPRequestOperationManager
更容易;)