json - 如何检测 Alamofire 请求何时为空?

标签 json swift swift3 alamofire

我正在使用 Alamofire 在我的 Swift 应用程序上执行我的 API 请求,它运行良好,但我还想检测 JSON 响应何时等于

我已经尝试比较对 nilNSNull 的响应,但这些都不适合我。我也尝试过使用 JSON.empty 但它似乎也不起作用。此外,我在我的 switch 应用程序上创建了一个 default 选项,试图捕获不是 successfailure 的选项>.

实际上我只维护了 JSON.empty 选项,但它从未进入 else 语句。

这是我现在拥有的代码:

Alamofire.request(encodedUrl!, method: .get, parameters: nil, encoding: JSONEncoding.default, headers: nil).responseJSON { response in

   switch(response.result) {
       case .success(_):
          if let JSON = response.result.value as? [[String : AnyObject]]{
              if JSON.isEmpty == false{
                  //Here the code if the request returns data
              }else{
                  //Here I wanted to use the code if null is retrieved
              }
          }else{
              //The JSON cannot be converted
          }
          break

       case .failure(_):
          //Failure 
          break
   } 
}

如何处理 Alamofire 上的 null 响应?

提前致谢!

最佳答案

根据您的代码,它会遇到 //The JSON cannot be converted since null can't be cast to [[String: AnyObject ]]

关于json - 如何检测 Alamofire 请求何时为空?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41244678/

相关文章:

java - 在将字符串写入 Apache Commons PropertiesConfiguration 之前对其进行转义的正确方法是什么?

java - 安卓 :-Consume a web service through Post method as a json Request and Response

ios - 使用 nil 必需属性保存上下文时,Core Data 自定义访问器崩溃

uilabel - TTTAttributedLabel 可点击链接在 Swift 3 中不起作用

json - 如何在 Lucerne 中捕获解析错误(常见的 lisp)

ios - 如何使用 Moya 取消多个网络请求

ios - 如何在 iOS 中将文本字段分隔为数字格式逗号?

core-data - 核心数据: How to write "generic" fetch/delete function in swift 3?

ios - 单击 UIButton 中的动画图标

javascript - Jquery从php/json文件加载数据并将新内容附加到div以进行聊天