ios - 如何在 Swift3 中将 Any 转换为字典

标签 ios swift3

我使用 Alamofire 网络请求,想处理错误消息,

我的代码:

class NetWorkingEngine: NSObject {
    typealias CreateNetWorkBlockSuccess = (_ responseobject:Any) -> ();
    typealias CreateNetWorkBlockFail = (_ responseobject:NSDictionary) -> ();

    func getDataFun(URL:String,netWorkingBlockSuccess:@escaping CreateNetWorkBlockSuccess,netWorkingBlockField:@escaping CreateNetWorkBlockField) -> Void {
        Alamofire.request(URL).responseJSON { (responseObject) in
            if responseObject.result.isSuccess {
                netWorkingBlockSuccess(responseObject.data!);
            }else{
                netWorkingBlockFail(responseObject.result);
            }
        }
    }
}

但要排队

netWorkingBlockFail(responseObject.result)

错误

cannot convert value of type “Result<Any>” to expected argument type "NSDictionary"

我该怎么办?

更新: I want to resquert Error Info, if you error request,Error info is 'Any',But how to 'Error info' convert Dictionary?

最佳答案

您可以使用 [:] 类型将 Any 类型转换为 Dictionary。假设你有一个名为 person 的变量,它是 Any 类型;然后使用以下代码:

let personDictionary = (person as! [String:String])["name"]

...其中 name 是字典中的键。

关于ios - 如何在 Swift3 中将 Any 转换为字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42799799/

相关文章:

ios - 我可以嵌入导航 Controller View 以占用自定义 UIViewController 的部分屏幕吗?

html - 在 ios 中加载 html 时,如何在 wkwebview 中使用 src 隐藏图像?

ios - 在 TableView 单元格 swift 3 中添加 Collection View

ios - 如何将unichar转换为character

objective-c - iOS - 第一个应用程序问题

ios - 点击按钮上的命令在 UIAutomation 中不起作用

ios - 以编程方式用箭头拉回按钮?

ios - Swift3 - Init 的使用不明确

swift3 - 本地通知在模拟器 iOS 10.0 中不起作用

iOS 开发 Swift 自定义单元