Skip to content

How to decode data to generic type? #181

Discussion options

You must be logged in to vote

something similar happened to me and I was able to solve it by implementing this piece of code: https://github.com/orgs/skiptools/discussions/11#discussioncomment-11249025

@inline(__always) func fetchData<T>(_ type: T.Type, endpoint: String) async throws -> T where T: Decodable {
    ...

    do {
        ...
        return try decoder.decode(type, from: data)
    } catch {
        ...
    }
}

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@marcprux
Comment options

Answer selected by marcprux
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants