ios - 使用 "po"从控制台日志中的服务器响应中获取数据

标签 ios swift dictionary po

我知道这是我要问的非常基本的问题,但我需要问我如何访问从服务器获取的字典数据。

这是我的回复

JSON: {
message = "The email must be a valid email address.";}

enter image description here

现在我想在控制台日志中执行“po”,所以在 po 语句中写什么

谢谢

最佳答案

所有你需要的类型

po yourResponseAsDictionary["message"]

已更新

抱歉,我以为你已经转换过了。

如果您没有使用 SwiftyJSONObjectMapper 之类的东西来解析您的 json 数据,那么您可以执行以下操作。 但我建议使用一些库将响应直接转换为您的模型

let yourResponseFromNetwork = "{ \"country_code\" : \"+9\", \"user_id\" : 123456}"
if let data = yourResponseFromNetwork.data(using: String.Encoding.utf8) {
    do {
        if let dic = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String:Any] {
            let countryCode = dic["country_code"]
            let userId = dic["user_id"]
        }
    } catch {
        print("Error occurred")
    }
}

关于ios - 使用 "po"从控制台日志中的服务器响应中获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47808669/

相关文章:

ios - 动画切换 CollectionViewLayout

ios - AppStore 拒绝 : use of private calls refers to my own methods

ios - Error Domain = NSCocoaErrorDomain Code=3840 "The operation couldn’ t 完成。 ( cocoa 错误 3840。)

swift - 查找 swift 框架中的新增功能

swift - 存储不同列表数据的最佳方式是什么?

sqlite - 适用于python的线程安全持久键值存储

ios - 无法在捆绑 CocoaPods 中加载 NIB

ios - UI 元素在 UIScrollView swift 中不起作用

python - python 字典中重复值的列表

css - 在css中制作背景图像贴图