Varun Kumar Varun Kumar - 5 months ago 34
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: @"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];
}

Answer

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.