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
46: Conditional Conformance
You’ve likely run into a situation where you're building a generic wrapper—maybe a Result type, a Box, or a network Response—and you suddenly realize you want that wrapper to be Equatable. It seems like a no-brainer. If the thing inside the wrapper can be compared, the wrapper itself should be comparable, right?
But as soon as you try to implement it, you hit a wall. I'll show you what I mean using a typical API response wrapper.
struct APIResponse<T> {
let payload: T
let statusCode: Int
}
// This is where we hit the snag
extension APIResponse: Equatable {
static func == (lhs: APIResponse, rhs: APIResponse) -> Bool {
return lhs.statusCode == rhs.statusCode && lhs.payload == rhs.payload
}
}
The wall you'll hit with blanket conformance
If you try to compile the code above, Swift is going to complain loudly. It will tell you that T does not conform to Equatable. The compiler is being pedantic for a good reason: you've told it that APIResponse is Equatable regardless of what T is. If I decide to use APIResponse<SomeNonEquatableClass>, your == implementation is suddenly impossible to execute because the payload can't be compared.
Now, the "naive" way to fix this is to just force T to be Equatable at the struct definition level: struct APIResponse<T: Equatable>. I've seen a lot of developers do this early in their careers. The problem is that you've just nuked the flexibility of your type. Now, you can't even create an APIResponse for a type that isn't equatable, even if you don't actually care about comparing those responses. You're restricting the use of your entire type just to satisfy one protocol.
Letting the compiler decide with conditional conformance
This is where conditional conformance comes in. Instead of forcing the restriction on the type itself, we apply the restriction only to the protocol conformance. We essentially tell Swift: "This type is Equatable, but only in the specific cases where the generic payload is also Equatable."
struct APIResponse<T> {
let payload: T
let statusCode: Int
}
// Now we are being precise
extension APIResponse: Equatable where T: Equatable {
static func == (lhs: APIResponse, rhs: APIResponse) -> Bool {
return lhs.statusCode == rhs.statusCode && lhs.payload == rhs.payload
}
}
This is a much more elegant contract. If you have an APIResponse<Int>, you can compare two of them. If you have an APIResponse<UIImage> (which isn't Equatable), the code still compiles and you can still use the struct—you just can't use the == operator on it. It’s the best of both worlds.
The trade-off in API discoverability
There is one thing to keep in mind here: it can occasionally confuse the people using your code. Because the conformance is conditional, a developer might see APIResponse in the documentation and assume it's always Equatable, only to find that their specific instance isn't.
I personally think this is a price worth paying. It's far better to have a flexible type that gains capabilities as its constraints are met than a rigid type that prevents you from using it in common scenarios. You'll see this pattern everywhere in the Swift Standard Library—Array is a prime example. An Array isn't always Equatable; it's only Equatable if the elements it holds are Equatable. Why reinvent the wheel when the language provides this exact mechanism?
📋 Practical Task
Implementing Hashable for a Generic Cache Wrapper
You are building a caching system. You have a generic wrapper called CacheEntry<T> that stores a value and a timestamp of when it was cached. You want to be able to use CacheEntry as a key in a Set or as a key in a Dictionary, which requires the type to conform to Hashable.
Your Task:
- Create a struct
CacheEntry<T>with two properties:value: Tandtimestamp: Date. - Implement
HashableforCacheEntryusing conditional conformance, so that it is onlyHashableifTis alsoHashable. - Verify your implementation by creating a
SetofCacheEntry<String>(this should work) and attempting to create aSetofCacheEntry<UIView>(this should fail to compile).
There are no comments for now.