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.
Update regarding Alamofire 5:
responseJSONDecodable
.Alamofire 4 won't add Codable support for now (see #2177), you can use this extension instead: https://github.com/Otbivnoe/CodableAlamofire.
Sample: http://swift.sandbox.bluemix.net/#/repl/59a4b4fad129044611590820
Using CodableAlamofire:
keypath
corresponds to the path where the results are contained in the JSON structure. E.g:keypath
=>results.persons
keypath
=>nil
(emptykeypath
throws an exception)