Swift 4 Codable Array's

前端 未结 4 1204
半阙折子戏
半阙折子戏 2021-02-02 14:04

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

[
    {\"firstname\": \"Tom\", \"lastname\": \"Smith\", \"age\": 31},
    {\"firstname\         


        
4条回答
  •  生来不讨喜
    2021-02-02 14:50

    to decode to array, you've your response in a type alias for clarity:

    typealias ServiceResponseObject = [ResponseObject]

    but then you'll have to confirm Array to codable:

    extension Array: Decodable where Element: Decodable {}

    that should make it all work.

提交回复
热议问题