Swift 3 Alamofire JSON response



我已经进入了我得到响应并能够将数据打印到Xcode中的控制台的阶段,但我不知道如何对数据进行排序。例如,我有4种发送回客户端的类型,我希望能够将它们设置为用于标签或其他元素的变量。

Alamofire.request(url, method: .post, encoding:     JSONEncoding.default)
        .responseJSON { response in
            print(response)
        }

您应该真正搜索此内容。您可以找到多次问的类似问题。

HIRES是一个例子

        var name : String?
        var age : Int?
        var stupid : Bool?
        var ageString : String?

    Alamofire.request(url, method: .post, encoding:     JSONEncoding.default)
            .responseJSON { response in
               if response != nil {
                  name = response.name
                  age = response.age
                  stupid = response.stupid
               }
            }
//    If you want a response to be converted and stored, you can do this
             ageString = String(response.age)
//    To use this value on a label 
           nameLabel.text = name
           ageLabel.text = ageString

最新更新