From the course: Learning Combine with Swift

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Solution: Use Combine to call an API endpoint

Solution: Use Combine to call an API endpoint - Swift Tutorial

From the course: Learning Combine with Swift

Start my 1-month free trial

Solution: Use Combine to call an API endpoint

(upbeat music) - [Instructor] So there are many ways in which you could tackle this, but I will show you the approach I took. On line 20, I created a dataTaskPublisher. I did the following, let publisher = URLSession.shared.dataTaskPublisher (for: url) and I passed in the URL from the previous line. Underneath, I entered .map{$0.data} to filter out just the data, and then I did .decode. For the type, I entered [Post] array .self and for decoder, JSONDecoder. Underneath that, I once again entered .map{$0.first}, to get just the first elements. Underneath that, I did .replaceNil with, and on line 16 I have an emptyPost sample, which I'm going to include here, emptyPost, and this is what will show if we get nothing, and underneath, I did compactMap{$0.title} to get just the title from the first post, and now we're ready to create a subscriber. On line 28, I did the following, let cancellableSink = publisher .sink,…

Contents