以下代码发出警告:
将'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];
}
答案 0 :(得分:4)
在您的头文件中确认<NSURLSessionDelegate>
,它将解决我认为的问题,
@interface ViewController : UIViewController <NSURLSessionDataDelegate>
这是因为您要在NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
中设置委托给自己,因此需要确认NSURLSessionDataDelegate
协议。