2016-01-21 48 views
1

我得到以下错误,而试图返回responseString在NSURLSession错误返回值

不兼容的块指针类型发送 '的NSString *(^)(NSData的* __强,NSURLResponse * __强,NSError * __强)' 到的参数类型 '无效(^)(NSData的* __强,NSURLResponse * __强,NSError * __强)'

在那里我把它从假设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/[email protected]&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]; 
} 

回答

2

您可以使用此方法块这样的:

-(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/[email protected]&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; 
}]; 
+0

请检查我的编辑问题,我需要从不同的类调用此API,然后我如何获得该类中的返回响应? – iphonemaclover

+0

@iphonemaclover他已经给你看了。看看他答案中的最后一段代码。这是你如何做, –

+0

thanx technerd,它的工作像一个魅力(y) – iphonemaclover

1

您试图从不返回任何值的块返回字符串。这就是编译器给你错误的原因。该块将被异步执行,所以你在做什么没有意义。你真的需要熟悉块的工作方式,特别是当它们异步执行时。

阅读:Working with blocks

+0

如何解决?我需要返回一个字符串一次,我得到的回应 – iphonemaclover

+0

你将无法从该函数返回一个字符串。继续执行 –

+0

块,例如,可以从该块发布通知,并将响应字符串放入通知中。你明白吗? –