Swift
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Optionals
-
Section 4: Object-Oriented and Value Types
-
Section 5: Memory Management
-
Section 6: Generics and Error Handling
-
Section 7: Concurrency
-
Section 8: Working with Collections
-
Section 9: Codable and Data Handling
-
Section 10: Protocol-Oriented Programming
-
Section 11: Testing and Tooling
-
Section 12: Practical Projects
-
Section 13: Interview Practice
-
Section 14: More Practice Exercises
-
Section 15: More Standard Library
-
Section 16: Advanced Concurrency
-
Section 17: Foundation Framework Deep Dive
-
Section 18: URLSession and Networking Deep Dive
-
Section 19: Combine Framework
-
Section 20: SwiftUI Fundamentals for Swift Developers
-
Section 21: Server-Side Swift with Vapor
-
Section 22: Swift Package Manager Deep Dive
-
Section 23: Swift Concurrency Deep Dive
-
Section 24: More Language Features
-
Section 25: Error Handling Deep Dive
-
Section 26: Testing Deep Dive
-
Section 27: Data Structures and Algorithms in Swift
-
Section 28: More Practice Exercises
-
Section 29: More Interview Practice
-
Section 30: Swift Macros (Swift 5.9+)
-
Section 31: Property Wrappers Ecosystem
-
Section 32: Swift Interop Deep Dive
-
Section 33: iOS App Architecture Patterns
-
Section 34: Performance and Debugging
-
Section 35: App Distribution and CI/CD
-
Section 36: More Practical Projects
-
Section 37: SwiftData and Persistence
-
Section 38: More Design Patterns
-
Section 39: More Review and Practice
-
Section 40: More Foundation Deep Dive
-
Section 41: Advanced Collections in Swift
-
Section 42: Advanced Generics Practice
-
Section 43: UIKit for Legacy and Hybrid Apps
-
Section 44: watchOS and visionOS Development Basics
-
Section 45: More Networking Patterns
-
Section 46: More Testing Practice
-
Section 47: Accessibility in Swift Apps
-
Section 48: Localization
-
Section 49: More Practical Projects Round 2
-
Section 50: Swift Charts Framework
-
Section 51: More Interview and Algorithm Practice
-
Section 52: Final Practice and Mastery
-
Section 53: Swift Compiler and Build System
-
Section 54: More Concurrency Practice
-
Section 55: App Store Guidelines and Review
-
Section 56: More Design and Architecture
86: Operators: map, filter, combineLatest
I see this a lot when developers first move toward reactive programming in Swift. They understand the concept of a "stream," but they treat the sink (the end of the pipeline) like a giant do { ... } block where all the business logic lives. It works, but it's brittle and hard to test.
Logic Leaking Into the Sink
// A common, messy approach to a search feature
searchTermsPublisher
.combineLatest(filterEnabledPublisher)
.sink { term, isEnabled in
// 🚩 The "Everything-in-the-Sink" mistake
if term.count >= 3 && isEnabled {
let request = SearchRequest(query: term, active: isEnabled)
self.apiClient.execute(request)
} else {
print("Search criteria not met")
}
}
If you're looking at this and thinking, "But it works," you're right. It does. But the sink is now doing three different jobs: it's filtering out short strings, checking a toggle state, and transforming a string into a request object. This is exactly where bugs hide. If you decide later that "Search" should also depend on a category selection, your sink becomes a nested nightmare of if-else statements.
Refining the Stream with Filter and Map
The goal of operators like filter and map is to ensure that by the time the data reaches your sink, it is already "correct." The sink should only be responsible for the final side effect—in this case, calling the API.
First, we use filter. This acts as a gatekeeper. If the condition isn't met, the data simply stops moving down the pipe. No else block is needed because the rest of the chain just doesn't execute. Then, we use map to transform the raw values into the object the API actually needs.
searchTermsPublisher
.filter { $0.count >= 3 } // Only let valid strings through
.combineLatest(filterEnabledPublisher)
.filter { _, isEnabled in isEnabled } // Only let through if toggle is ON
.map { term, _ in
return SearchRequest(query: term, active: true)
}
.sink { request in
// Now the sink is clean. It just does one thing.
self.apiClient.execute(request)
}
Merging Dependencies with combineLatest
You might be wondering why I used combineLatest instead of something like zip. This is a crucial distinction. zip requires a pair—it waits for both publishers to emit a new value before it sends anything. If the user types ten characters but never touches the "Filter Enabled" toggle, zip would stay silent.
combineLatest is different. Once every publisher in the group has emitted at least one value, it will fire every time any of them change. This is exactly what you want for UI state. If the user changes the toggle, you want the search to potentially trigger immediately using the last known search term.
One thing to watch out for: combineLatest won't emit anything until all participating publishers have sent at least one value. If your filterEnabledPublisher doesn't have an initial value (like a CurrentValueSubject), your search will feel broken because the first few keystrokes will be ignored until that toggle is flipped for the first time.
📋 Practical Task
Building a Reactive Form Validator
You are building a registration form. The "Create Account" button should only be enabled when the following conditions are met:
- The email address contains an "@" symbol.
- The password is at least 8 characters long.
- The "Terms of Service" checkbox is checked (true).
Your Task: Create a pipeline using combineLatest, map, and filter (or boolean logic within a map) that takes three publishers—emailPublisher, passwordPublisher, and tosPublisher—and emits a single Boolean value to a sink that updates the submitButton.isEnabled property.
Avoid putting if/else logic inside the sink. The sink should only contain one line: self.submitButton.isEnabled = isValid.
There are no comments for now.