在NSURLSession中返回错误
尝试返回responseString时出现错误
('NSData * __ strong,NSURLResponse * __ strong,NSError * __ strong)types的参数发送'NSString *(^)(NSData * __ strong,NSURLResponse * __ strong,NSError * __ strong)'的不兼容块指针types
我从哪里调用它从AViewController类
NSString *responseString=[self callAPI];
下面是我在BViewModel类中的代码:
-(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]; }
你可以像这样使用这个方法:
-(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]; }
调用这个方法并返回响应如下:
[self callAPIWithCompletionHandler:^(NSString *strResponse) { NSString *responseString = strResponse; }];
假设这个方法在ViewControllerA
实现,并且你想从ViewControllerB
调用它。
然后在ViewControllerB
导入ViewControllerA
,并在ViewControllerB
制作ViewControllerA
实例。
ViewControllerA *vcA = [[ViewControllerA alloc] init]; [vcA callAPIWithCompletionHandler:^(NSString *strResponse) { NSString *responseString = strResponse; }];
你正试图从一个不应该返回任何值的块返回一个string。 这就是编译器给你错误的原因。 该块将被asynchronous执行,所以你在做什么没有意义。 你真的需要熟悉块如何工作,特别是当它们asynchronous执行时。
阅读: 使用块