view raw
Varun Kumar Varun Kumar - 11 months ago 63
iOS Question

How to solve warning message: Sending 'viewController const __strong to parameter of incompatible type 'id<nsurlsessiondelegate>

The following code gave a warning of :

Sending 'viewController const __strong to parameter of incompatible
type 'id

Here is my code:

- (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: @""];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url

[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(@"RESPONSE: %@", [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]);

completionHandler(NO, nil, error);

[postDataTask resume];


Confirm <NSURLSessionDelegate> in your header file and it will solve the issue i think,

  @interface ViewController : UIViewController <NSURLSessionDataDelegate>

It is because you are setting delegate to self in NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil]; So it's requires to confirm NSURLSessionDataDelegate protocol.