2016-01-21 139 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

塊,例如,可以從該塊發佈通知,並將響應字符串放入通知中。你明白嗎? –