从 NSURLConnection 不兼容的指针类型迁移到 NSURLSession
Migrating to NSURLSession from NSURLConnection incompatible pointer types
我正在从 NSURLConnection
移动到 NSURLSession
和 运行 遇到 NSMutableURLRequest
的问题,该 NSMutableURLRequest
保存 URL 请求的值。
关于如何在 NSURLSession
中保留此信息而不出现此错误的任何想法:
incompatible pointer types sending nsmutableurlrequest to parameter of
type nesting * _nonull nsurl urlwithstring
我看到这一行 [NSURL URLWithString:myRequest]
需要一个 NSString,但我如何仍然传递我 NSMutableURLRequest
上的其他信息?
NSMutableURLRequest *myRequest = [NSMutableURLRequest requestWithURL: myURL];
[acquisitionRequest setValue:userAgent forHTTPHeaderField:@"User-Agent"];
NSURLSession *session = [NSURLSession sharedSession];
[[session dataTaskWithURL:[NSURL URLWithString:myRequest]
completionHandler:^(NSData *data,
NSURLResponse *response,
NSError *error) {
}] resume];
您应该使用 dataTaskWithRequest:completionHandler:
而不是 dataTaskWithURL:completionHandler:
。这样你就可以传递整个请求。如果它抱怨可变,你可以复制它来摆脱可变状态。
NSMutableURLRequest *myRequest = [NSMutableURLRequest requestWithURL: myURL];
[acquisitionRequest setValue:userAgent forHTTPHeaderField:@"User-Agent"];
NSURLSession *session = [NSURLSession sharedSession];
[[session dataTaskWithRequest:[myRequest copy]
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
// completion stuff
}] resume];
您的问题是您在使用 URLWithString:
创建 URL 时传递了 NSMutableURLRequest
对象,而这期望您传递 NSString
。通过 myURL
而不是 [NSURL URLWithString:myRequest]
,你应该是好的。
不过,我也建议改用 dataTaskWithRequest:completionHandler:
。
我收到了类似的问题。我无法登录 iOS 9,因为 NSData 已被弃用。我需要更新到 NSURLSession。我正在尝试使用下面的代码这样做,但是 - 似乎仍然有两条线从 NSData 绘制,其中我指向它们的指针是一个 NSURLSession。
它正在加重系统。如何设置接收线路打开以连接 NSURLSession ?
NSURLSession *session=[NSURLSession sharedSession];
NSLog(@"response%@",response);
NSLog(@"Response code: %ld", (long)[response statusCode]);
if ([response statusCode] >= 200 && [response statusCode] < 300)
{
NSString *responseData = [[NSString alloc]initWithData:session encoding:NSUTF8StringEncoding];
NSLog(@"Response ==> %@", responseData);
NSError *error = nil;
/* NSDictionary *jsonData = [NSJSONSerialization
JSONObjectWithData:urlData
options:NSJSONReadingMutableContainers
error:&error];*/
// NSLog(@"dict%@",jsonData);
self.topLevelArray3 = [NSJSONSerialization JSONObjectWithData:session options:NSJSONReadingMutableContainers error:&error];
for (int i=0; i<[self.topLevelArray3 count]; i++) {
我正在从 NSURLConnection
移动到 NSURLSession
和 运行 遇到 NSMutableURLRequest
的问题,该 NSMutableURLRequest
保存 URL 请求的值。
关于如何在 NSURLSession
中保留此信息而不出现此错误的任何想法:
incompatible pointer types sending nsmutableurlrequest to parameter of type nesting * _nonull nsurl urlwithstring
我看到这一行 [NSURL URLWithString:myRequest]
需要一个 NSString,但我如何仍然传递我 NSMutableURLRequest
上的其他信息?
NSMutableURLRequest *myRequest = [NSMutableURLRequest requestWithURL: myURL];
[acquisitionRequest setValue:userAgent forHTTPHeaderField:@"User-Agent"];
NSURLSession *session = [NSURLSession sharedSession];
[[session dataTaskWithURL:[NSURL URLWithString:myRequest]
completionHandler:^(NSData *data,
NSURLResponse *response,
NSError *error) {
}] resume];
您应该使用 dataTaskWithRequest:completionHandler:
而不是 dataTaskWithURL:completionHandler:
。这样你就可以传递整个请求。如果它抱怨可变,你可以复制它来摆脱可变状态。
NSMutableURLRequest *myRequest = [NSMutableURLRequest requestWithURL: myURL];
[acquisitionRequest setValue:userAgent forHTTPHeaderField:@"User-Agent"];
NSURLSession *session = [NSURLSession sharedSession];
[[session dataTaskWithRequest:[myRequest copy]
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
// completion stuff
}] resume];
您的问题是您在使用 URLWithString:
创建 URL 时传递了 NSMutableURLRequest
对象,而这期望您传递 NSString
。通过 myURL
而不是 [NSURL URLWithString:myRequest]
,你应该是好的。
不过,我也建议改用 dataTaskWithRequest:completionHandler:
。
我收到了类似的问题。我无法登录 iOS 9,因为 NSData 已被弃用。我需要更新到 NSURLSession。我正在尝试使用下面的代码这样做,但是 - 似乎仍然有两条线从 NSData 绘制,其中我指向它们的指针是一个 NSURLSession。
它正在加重系统。如何设置接收线路打开以连接 NSURLSession ?
NSURLSession *session=[NSURLSession sharedSession];
NSLog(@"response%@",response);
NSLog(@"Response code: %ld", (long)[response statusCode]);
if ([response statusCode] >= 200 && [response statusCode] < 300)
{
NSString *responseData = [[NSString alloc]initWithData:session encoding:NSUTF8StringEncoding];
NSLog(@"Response ==> %@", responseData);
NSError *error = nil;
/* NSDictionary *jsonData = [NSJSONSerialization
JSONObjectWithData:urlData
options:NSJSONReadingMutableContainers
error:&error];*/
// NSLog(@"dict%@",jsonData);
self.topLevelArray3 = [NSJSONSerialization JSONObjectWithData:session options:NSJSONReadingMutableContainers error:&error];
for (int i=0; i<[self.topLevelArray3 count]; i++) {