如何解决警告消息:将 'viewController const __strong 发送到不兼容类型 'id<nsurlsessiondelegate> 的参数

Var*_*mar 3 objective-c ios

以下代码给出了警告:

将“viewController const __strong”发送到不兼容类型“id”的参数

这是我的代码:

- (void)postRequestWithParam: (NSDictionary *)param onCompletion: (CompletionHandler)completionHandler

{
NSError *error;


NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];

configuration.timeoutIntervalForRequest = 30.0;

NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];



NSURL *url = [NSURL URLWithString: @"http://test.demo.net/demoapp/index.php"];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
                                                       cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                   timeoutInterval:60.0];

[request setHTTPMethod:@"POST"];

if(param == nil)

    param = [NSDictionary dictionary];


NSData *postData = [NSJSONSerialization dataWithJSONObject:param options:0 error:&error];

NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];


[request setValue:postLength forHTTPHeaderField:@"Content-Length"];

[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];


[request setHTTPBody:postData];


NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request
                                                completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
                                      {
                                          if (!error)
                                          {
                                              completionHandler(YES, [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error], nil);

                                              NSString *responseStr = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
                                              NSLog(@"res...%@",responseStr);

                                              NSLog(@"RESPONSE:  %@", [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]);
                                          }

                                          else
                                          {
                                              completionHandler(NO, nil, error);
                                          }
                                      }];

[postDataTask resume];
}
Run Code Online (Sandbox Code Playgroud)

Lio*_*ion 5

<NSURLSessionDelegate>在你的头文件中确认,它会解决我认为的问题,

  @interface ViewController : UIViewController <NSURLSessionDataDelegate>
Run Code Online (Sandbox Code Playgroud)

这是因为您将委托设置为 selfNSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];所以需要确认NSURLSessionDataDelegate协议。