My code is below:
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];
When I use AFHTTPRequestOperationManager
, I used NSDictionary
as parameter for GET method.
So I thought [request setValuesForKeysWithDictionary:[filter filteringDictionary]];
would be corresponding.
But I got an error :
setValue:forUndefinedKey:]: this class is not key value coding-compliant for name(ex)
I architected the parameter is NSDictionary
for convenience...
Can't I set parameter as NSDictionary
when using AFHTTPRequestOperation
, not AFHTTPRequestOperationManager
?
Help me please
AFHTTPRequestOperation
takes an NSURLRequest
as an init parameter. As such, you need to handle the serialization yourself, append it to your url, and then encode the url:
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[yourUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
Just one more reason why AFHTTPRequestOperationManager
is easier ;)