Search code examples
swifttypesdecodable

Array of Decodable type is not decodable


If we have a type Foo: Decodable how can we make Array<Foo> decodable?

Would I have to create another type Foos: Decodable ?

If so how would this work?


I'm seeing the issue here

func exampleMethod<T:Decodable>(type: T) { }

// Argument type 'Array<Foo>.Type' does not conform to expected type 'Decodable'
exampleMethod(type: [Foo].self)

Solution

  • Just use this:

    let res = JSONDecoder().decode([Foo].self, data)

    for your case:

    exampleMethod(type: [Foo()])

    Regarding exampleMethod signature you should write an array. Not a type of array.