Last active
July 9, 2019 20:52
-
-
Save erikbasargin/1c2820237761e6dee2641cfd78a320a1 to your computer and use it in GitHub Desktop.
Fix Publisher.decode for JSONDecoder and TopLevelDecoder π
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) | |
extension Publisher where Output == URLSession.DataTaskPublisher.Output { | |
public func decode<Item, Coder>(type: Item.Type, decoder: Coder) -> Publishers.TryMap<Self, Item> where Item: Decodable, Coder: TopLevelDecoder, Coder.Input == Data { | |
return tryMap { try decoder.decode(type, from: $0.data) } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example code with this error:
Someone solves the problem like this:
Or like this:
But these are not fun solutions to the problem. That's why this gist appeared. Good luck! ππ