- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试阅读 NSHTTPURLResponse
状态代码,但是 NSHTTPURLResponse
返回零,但没有 NSError
已创建。
这在 iOS 11 之前有效,但我没有收到关于它已被弃用的警告,而且我无法在网上找到任何引用 NSURLSession
的内容有这个问题。
知道为什么吗?
我通常调用以下方法 [MyClass getHTTPResponseRequest:@"http://www.google.com/"];
+ (NSInteger) getHTTPResponseRequest : (NSString*) testURL {
__block NSHTTPURLResponse * r = nil;
[[[NSURLSession sharedSession] dataTaskWithURL:[NSURL URLWithString:testURL]
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error) {
NSLog(@"Error %@", error.localizedDescription);
}
r = (NSHTTPURLResponse *)response;
}] resume];
if(r == nil){
NSLog(@"Response is nil");
return 9999;
}
return r.statusCode;
}
最佳答案
这在 iOS 11 之前也行不通。 dataTaskWithURL
完成处理程序被异步调用,但您不会在尝试返回 statusCode
之前等待请求完成。
您应该采用异步模式,例如自己使用完成处理程序模式:
+ (void)getHTTPResponseRequestWithURL:(NSString *)urlString completion:(void(^ _Nonnull)(NSInteger))completion {
NSURL *url = [NSURL URLWithString:urlString];
[[[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error) {
NSLog(@"Error %@", error.localizedDescription);
}
NSInteger statusCode;
if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
statusCode = [(NSHTTPURLResponse *)response statusCode];
} else {
statusCode = 9999;
}
completion(statusCode);
}] resume];
}
你会这样调用它:
[MyClass getHTTPResponseRequestWithURL:@"http://google.com" completion:^(NSInteger statusCode) {
// examine the `statusCode` here
NSLog(@"%ld", (long)statusCode);
}];
// but the above runs asynchronously, so you won't have `statusCode` here
现在,显然您的完成处理程序参数通常会返回比整数 statusCode
更有意义的东西,但它说明了这个想法:不要尝试在不使用异步方法的情况下从异步方法返回值异步模式(例如完成处理程序)。
关于ios - NSHTTPURLResponse 为 nil 但未生成 NSError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47893916/
我意识到这与此处的现有帖子相似,What's the Point of (NSError**)error? ,但我的问题有点不同。我了解双指针的工作原理,以及这是常见的 iOS API 错误模式。我的
我刚刚升级到 Xcode 6.3 和 Swift 1.2,并且正在更新语法。在我的项目中,我使用 TCBlobDownloadSwift用于一些常规下载任务。更新后,我从框架的以下方法中收到编译器错误
在下面的代码中NSError实例化(match是GameCenter的GKMatch): ... NSString * deviceID = appDelegate.deviceID;
我开发了一个 ios 应用程序,它有: NSError *error; 代替: NSError *error = nil; 当我在模拟器中调试和连接时在设备上调试时,它工作正常。在我将其存档并将其发
在我的 iOS 应用程序中,我需要播放声音。我有以下代码: var audioFileLocationUrl = NSBundle.mainBundle().pathForResource("midn
我正在使用外部组件连接到 SOAP 服务。当我收到结果时,它作为对象 value 返回,它可以包含 NSError 或结果对象。 如果出现错误,我想提取错误代码和消息。如何将 value 转换为 NS
我有一个 NSError ** 存储在一个数组中(所以我可以这样得到它 array[0])。我正在尝试将其转换为变量: NSError * __autoreleasing *errorPointer
我需要另一双眼睛。 奇怪的是,我似乎无法访问自定义 NSError 的属性。我不断收到 EXC_BAD_ACCESS 错误。这是我的代码: if (response.isUnautho
我收到这个错误: incompatible pointer types assigning to NSError _strong from NSError _autoreleasing 这是我的头文件
我正在 swift 中使用 AFNetworking,并且在将以下代码转换为 swift 时遇到问题 objective-c [client GET:url parameters:nil succes
physicsWorld.gravity = CGVector(dx: 0, dy: -2) physicsWorld.contactDelegate = self motionMan
这个问题类似于ios NSError types但是那里描述的解决方案没有用,我相信这不是我所需要的。 我有一个执行异步调用然后调用完成 block 的方法。当我尝试将 NSError ** 传递给完
我试图在 if let jsonData Array = try NSJSONSerialization... 行中从 Swift 1 转换为 2,因为它最初给了我一个来自“额外参数‘错误’的错误”
最近我刚刚将 swift 2.3 项目转换为 3.2,alamofire 也得到了转换,我收到了很多问题,其中大部分都已解决,现在我被给定的两个问题所困扰 问题在 alamofire 的 Respon
在我使用该方法之前几次... NSURLResponse *response; NSError *error; NSData *data = [NSURLConnection sendSynchron
这个相对简单的enum,编译得很好,但是...... public protocol AuthKitErrorProtocol { var error: NSError { get } } e
你好, 我正在尝试为接受 *NSError 作为参数的代码设置一些单元测试。如果存在验证问题,则不会保存对象并设置 NSError 条件。 我的方法是: - (BOOL)validateConsist
我想我在 NSError 上发现了一个错误,但想运行它让你们通过,看看是否真的只是我做错了什么。 在向使用 userInfo 的 NSError 实例发送描述时发生崩溃。如果将 userInfo 设置
对于这个问题,我使用的是我的实际代码的精简版本。 该应用程序使用 MVVM 架构构建。假设有一个登录屏幕。有3个文件。 ApiClient 文件与服务器通信并获取响应。如果输入的用户名和密码匹配,它会
例如 NSError *error = nil; if ([something error:&error]) { // ... } // ... if ([somethingElse er
我是一名优秀的程序员,十分优秀!