Swift 4 Codable Array's

19,677

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 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)

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

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 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}
        ]
    }
}

keypath => results.persons

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

keypath => nil (empty keypath throws an exception)

Share:
19,677
Charlie Fish
Author by

Charlie Fish

Software Engineer (iOS @ ForeFlight) 🖥📱, student pilot ✈️, HUGE Colorado Avalanche fan 🥅, entrepreneur (rrainn, Inc.) ⭐️ Contact me ✉️, About me 👨🏻‍💻, Terms 📝, Website 🌐

Updated on June 17, 2022

Comments

  • Charlie Fish
    Charlie Fish about 2 years

    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}
    ]
    

    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.

    I'm also using Alamofire to handle the requests.

    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.

  • Charlie Fish
    Charlie Fish almost 7 years
    Very detailed answer. Thank you very much. Don't quite have time to look through it all now but will get to it at some point. Thanks so much!!!
  • Charlie Fish
    Charlie Fish over 6 years
    What is unique about your answer compared to the answer that is accepted?
  • abbawssdsad
    abbawssdsad over 6 years
    you don't need extension or anything. I mean it is very easy just to use the data response, from existing function. Simple alternative for people who already using Alamofire for long. Right?
  • Charlie Fish
    Charlie Fish over 6 years
    Yeah. Well after doing more research there are better ways to handle this. Adding responseCodable I feel like just adds complexity. I would also argue that my main question was about Swift 4 Codable Arrays. Not Alamofire. Although I mentioned Alamofire that was not my main question.
  • nathan
    nathan about 6 years
    Answer updated with Alamofire 5 code (no need for extensions)