Swift 4可编码阵列 [英] Swift 4 Codable Array's

查看:92
本文介绍了Swift 4可编码阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个API路由,该路由返回一个JSON对象数组。例如:

So I have an API route that returns a JSON array of objects. For example:

[
    {"firstname": "Tom", "lastname": "Smith", "age": 31},
    {"firstname": "Bob", "lastname": "Smith", "age": 28}
]

我正在尝试设想如何使用Swift中的新可编码功能将它们转换为类中的两个对象。因此,如果我有一个可编码的人员类,我想接受该响应并将它给我两个人对象。

I'm trying to envision how to use the new codable feature in Swift for to convert those into two objects in a class. So if I have a person class that is codable I would want to take that response and have it give me two person objects.

我还使用Alamofire来处理

I'm also using Alamofire to handle the requests.

我该怎么做?到目前为止,我所看到的与可编码内容相关的所有内容都只允许1个对象。而且我还没有看到与Alamofire或Web框架的任何集成。

How can I do this? So far everything I've seen related to the codable stuff only allows 1 object. And I haven't seen any integration with Alamofire or a web framework.

推荐答案

有关Alamofire 5的更新: responseJSONDecodable

Update regarding Alamofire 5: responseJSONDecodable.

struct Person: Codable {
    let firstName, lastName: String
    let age: Int

    enum CodingKeys : String, CodingKey {
        case firstName = "firstname"
        case lastName = "lastname"
        case age
    }
}

Alamofire.request(request).responseJSONDecodable { (response: DataResponse<Person>) in
    print(response)
}






Alamofire 4目前不会添加可编码支持(请参见< a href = https://github.com/Alamofire/Alamofire/issues/2177 rel = noreferrer>#2177 ),则可以改用以下扩展名: https://github.com/Otbivnoe/CodableAlamofire


Alamofire 4 won't add Codable support for now (see #2177), you can use this extension instead: https://github.com/Otbivnoe/CodableAlamofire.

let jsonData = """
[
    {"firstname": "Tom", "lastname": "Smith", "age": 31},
    {"firstname": "Bob", "lastname": "Smith", "age": 28}
]
""".data(using: .utf8)!

struct Person: Codable {
    let firstName, lastName: String
    let age: Int

    enum CodingKeys : String, CodingKey {
        case firstName = "firstname"
        case lastName = "lastname"
        case age
    }
}

let decoded = try! JSONDecoder().decode([Person].self, from: jsonData)

示例: http://swift.sandbox.bluemix.net/#/repl/59a4b4fad129044611590820

使用CodableAlamofire:

Using CodableAlamofire:

let decoder = JSONDecoder()
Alamofire.request(url).responseDecodableObject(keyPath: nil, decoder: decoder) { (response: DataResponse<[Person]>) in
    let persons = response.result.value
    print(persons)
}

keypath 对应于结果包含在JSON结构中的路径。例如:

keypath corresponds to the path where the results are contained in the JSON structure. E.g:

{
    "result": {
        "persons": [
            {"firstname": "Tom", "lastname": "Smith", "age": 31},
            {"firstname": "Bob", "lastname": "Smith", "age": 28}
        ]
    }
}

键路径 => results.persons

[
    {"firstname": "Tom", "lastname": "Smith", "age": 31},
    {"firstname": "Bob", "lastname": "Smith", "age": 28}
]

keypath => nil (空的 keypath 引发异常)

keypath => nil (empty keypath throws an exception)

这篇关于Swift 4可编码阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆