问题
I get below error while trying to return responseString
Incompatible block pointer types sending 'NSString *(^)(NSData *__strong, NSURLResponse *__strong, NSError *__strong)' to parameter of type 'void (^)(NSData *__strong, NSURLResponse *__strong, NSError *__strong)'
where i call it from Suppose AViewController Class
NSString *responseString=[self callAPI];
And below is my code in BViewModel class:
-(NSString* )callAPI
{
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
NSURL *url = [NSURL URLWithString:@"http://appersgroup.com/talkawalk/login.php?email=twalknet@gmail.com&password=123456&latitude=52.486245&longitude=13.327496&device_token=show"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
[request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request addValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setHTTPMethod:@"POST"];
/* NSDictionary *mapData = [[NSDictionary alloc] initWithObjectsAndKeys: @"TEST IOS", @"name",
@"IOS TYPE", @"typemap",
nil];
NSData *postData = [NSJSONSerialization dataWithJSONObject:mapData options:0 error:&error];
[request setHTTPBody:postData];
*/
NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSString* responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
if([responseString rangeOfString:@"nil"].location != NSNotFound)
{
NSString * newResponse = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
responseString = newResponse;
}
NSLog(@"%@",responseString);
NSLog(@"response %@",response);
NSLog(@"error %@",error);
return responseString;//adding this line give me error ? how to return value
}];
[postDataTask resume];
}
回答1:
You can use this method with block like this :
-(void)callAPIWithCompletionHandler : (void (^) (NSString * strResponse)) completionHandler
{
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
NSURL *url = [NSURL URLWithString:@"http://appersgroup.com/talkawalk/login.php?email=twalknet@gmail.com&password=123456&latitude=52.486245&longitude=13.327496&device_token=show"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
[request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request addValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setHTTPMethod:@"POST"];
/* NSDictionary *mapData = [[NSDictionary alloc] initWithObjectsAndKeys: @"TEST IOS", @"name",
@"IOS TYPE", @"typemap",
nil];
NSData *postData = [NSJSONSerialization dataWithJSONObject:mapData options:0 error:&error];
[request setHTTPBody:postData];
*/
NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSString* responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
if([responseString rangeOfString:@"nil"].location != NSNotFound)
{
NSString * newResponse = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
responseString = newResponse;
}
NSLog(@"%@",responseString);
NSLog(@"response %@",response);
NSLog(@"error %@",error);
completionHandler(responseString);
}];
[postDataTask resume];
}
Call this method and return response as below :
[self callAPIWithCompletionHandler:^(NSString *strResponse) {
NSString *responseString = strResponse;
}];
Suppose this method implemented in ViewControllerA
, and you want to call this from ViewControllerB
.
Then import ViewControllerA
in ViewControllerB
and make instance of ViewControllerA
in ViewControllerB
.
ViewControllerA *vcA = [[ViewControllerA alloc] init];
[vcA callAPIWithCompletionHandler:^(NSString *strResponse) {
NSString *responseString = strResponse;
}];
回答2:
You are trying to return a string from a block which is not supposed to return any value. This is why the compiler gives you the error. The block will be executed asynchronously, so there's no point in what you're doing. You really need to get familiar with how blocks work, especially when they're executed asynchronously.
Read this: Working with blocks
来源:https://stackoverflow.com/questions/34916511/return-value-in-nsurlsession-error