I have a method that makes a specific request to an api, that I would like to return the result from. Here is my method:
-(NSDictionary *) getProfileDataForUser:(NSString *)user_id {
NSURL *getProfileDataRequestURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@users/profile.php?user_id=%@", _apiRootUrl, user_id]];
NSMutableURLRequest *getProfileDataRequest = [NSMutableURLRequest requestWithURL:getProfileDataRequestURL cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:10.0f];
[getProfileDataRequest setHTTPMethod:@"GET"];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:getProfileDataRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
error = nil;
id jsonData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
if (jsonData != nil && error == nil) {
NSDictionary *responseDict = jsonData;
if ([responseDict objectForKey:@"user"]) {
// this is a dictionary
return [responseDict objectForKey:@"user"];
} else {
return responseDict;
}
}
else {
return @{};
}
}];
}
The trouble is I get multiple semantic issues:
Error on sendAsynchronousRequest
method:
Incompatible block pointer types sending 'id (^)(NSURLResponse *__strong, NSData *__strong, NSError *__strong)' to parameter of type 'void (^)(NSURLResponse *__strong, NSData *__strong, NSError *__strong)'
Error on returns:
Return type 'NSDictionary *' must match previous return type 'id' when block literal has unspecified explicit return type
I have tried changing the return type of my method to id, and just returning jsonData
but I still get the first semantic issue. How can I make this work?