swift
99 lines · 2 tabs
Sofia Martinez
Jan 2026
2 tabs
import Foundation
import Combine
class NetworkService {
private let baseURL = "https://api.example.com"
private var cancellables = Set<AnyCancellable>()
func fetchPosts() -> AnyPublisher<[Post], Error> {
guard let url = URL(string: "\(baseURL)/posts") else {
return Fail(error: URLError(.badURL))
.eraseToAnyPublisher()
}
return URLSession.shared.dataTaskPublisher(for: url)
.map(\.data)
.decode(type: PostsResponse.self, decoder: JSONDecoder())
.map { $0.posts }
.receive(on: DispatchQueue.main)
.eraseToAnyPublisher()
}
func searchPosts(query: String) -> AnyPublisher<[Post], Error> {
guard let url = URL(string: "\(baseURL)/posts/search?q=\(query)") else {
return Fail(error: URLError(.badURL))
.eraseToAnyPublisher()
}
return URLSession.shared.dataTaskPublisher(for: url)
.retry(3)
.map(\.data)
.decode(type: [Post].self, decoder: JSONDecoder())
.catch { error -> AnyPublisher<[Post], Error> in
print("Error fetching posts: \(error)")
return Just([])
.setFailureType(to: Error.self)
.eraseToAnyPublisher()
}
.receive(on: DispatchQueue.main)
.eraseToAnyPublisher()
}
}
struct PostsResponse: Decodable {
let posts: [Post]
}
struct Post: Decodable, Identifiable {
let id: Int
let title: String
let body: String
let authorId: Int
}
import Foundation
import Combine
class PostsViewModel: ObservableObject {
@Published var posts: [Post] = []
@Published var isLoading = false
@Published var errorMessage: String?
@Published var searchQuery = ""
private let networkService = NetworkService()
private var cancellables = Set<AnyCancellable>()
init() {
setupSearchDebounce()
}
func loadPosts() {
isLoading = true
errorMessage = nil
networkService.fetchPosts()
.sink(
receiveCompletion: { [weak self] completion in
self?.isLoading = false
if case .failure(let error) = completion {
self?.errorMessage = error.localizedDescription
}
},
receiveValue: { [weak self] posts in
self?.posts = posts
}
)
.store(in: &cancellables)
}
private func setupSearchDebounce() {
$searchQuery
.debounce(for: .milliseconds(300), scheduler: DispatchQueue.main)
.removeDuplicates()
.filter { !$0.isEmpty }
.flatMap { query in
self.networkService.searchPosts(query: query)
.catch { _ in Just([]) }
}
.assign(to: &$posts)
}
}
2 files · swift
Explain with highlit
Combine provides a declarative Swift API for processing values over time, perfect for handling async events like network requests, user input, and timers. Publishers emit sequences of values, and subscribers receive them. Operators transform, filter, and combine streams. I use URLSession.dataTaskPublisher for network calls, chaining operators like map, decode, and catch for transformation and error handling. The @Published property wrapper creates publishers automatically. Combine's sink and assign subscribers connect publishers to UI or state. Cancellables manage subscription lifecycles. This reactive approach eliminates callback hell and makes async code linear and composable.
Related snips
swift
import Combine
import Foundation
class SearchViewModel: ObservableObject {
@Published var searchQuery = ""
@Published var results: [SearchResult] = []
Combine operators for data transformation
swift
combine
reactive
by Sofia Martinez
1 tab
swift
import SwiftUI
struct ContentView: View {
@State private var username = ""
@State private var isLoggedIn = false
@StateObject private var viewModel = LoginViewModel()
SwiftUI declarative UI with state management
swift
swiftui
ios
by Sofia Martinez
2 tabs
swift
import SwiftUI
struct CardModifier: ViewModifier {
var backgroundColor: Color = .white
var cornerRadius: CGFloat = 12
var shadowRadius: CGFloat = 5
Custom SwiftUI view modifiers for reusability
swift
swiftui
ios
by Sofia Martinez
2 tabs
swift
import Foundation
import UIKit
class ImageProcessor {
// Background processing with main thread updates
func processImage(_ image: UIImage, completion: @escaping (UIImage?) -> Void) {
Grand Central Dispatch for concurrency
swift
gcd
concurrency
by Sofia Martinez
1 tab
swift
import SwiftUI
enum Route: Hashable {
case postDetail(id: Int)
case userProfile(id: Int)
case settings
Navigation patterns with NavigationStack
swift
swiftui
navigation
by Sofia Martinez
2 tabs
swift
import UIKit
import WebKit
class WebViewController: UIViewController {
private var webView: WKWebView!
private var progressView: UIProgressView!
WKWebView for web content display
swift
wkwebview
webkit
by Sofia Martinez
1 tab
Share this code
Here's the card — post it anywhere.