NSURLSession错误中的返回值 [英] Return value in NSURLSession Error
问题描述
尝试返回responseString时出现以下错误
I get below error while trying to return responseString
不兼容的块指针类型将'NSString *(^)(NSData * __ strong,NSURLResponse * __ strong,NSError * __ strong)'发送给类型为void(^)(NSData * __ strong,NSURLResponse * __ strong,NSError的参数* __ strong)'
我从AViewController类调用它的地方
where i call it from Suppose AViewController Class
NSString *responseString=[self callAPI];
下面是我在BViewModel类中的代码:
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];
}
推荐答案
您可以将这种方法与类似的代码块一起使用:
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;
}];
假设此方法在 ViewControllerA
中实现,并且您想从 ViewControllerB
调用此方法.
Suppose this method implemented in ViewControllerA
, and you want to call this from ViewControllerB
.
然后在 ViewControllerB
中导入 ViewControllerA
,并在 ViewControllerB
中创建 ViewControllerA
的实例.
Then import ViewControllerA
in ViewControllerB
and make instance of ViewControllerA
in ViewControllerB
.
ViewControllerA *vcA = [[ViewControllerA alloc] init];
[vcA callAPIWithCompletionHandler:^(NSString *strResponse) {
NSString *responseString = strResponse;
}];
这篇关于NSURLSession错误中的返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!