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
140: Property Wrappers with Projected Values
I remember reviewing a pull request from a developer who was building a complex registration form. They had created a property wrapper called @Validated to handle email and password checks. It worked great for cleaning up the data, but the developer had a problem: the UI needed to know if the field was currently invalid to show a red border, but the wrappedValue was just the string itself. To solve this, they were manually creating a second boolean property for every single form field—isEmailValid, isPasswordValid, isZipCodeValid—just to track the state. It was a mountain of boilerplate that completely defeated the purpose of using a property wrapper in the first place.
This is exactly where projected values come in. If the wrappedValue is the "meat" of your property, the projectedValue is the metadata or the "side-channel" that lets you expose extra information about that property without cluttering your object's main interface.
Beyond the Wrapped Value
You already know that when you apply a property wrapper, Swift generates a hidden storage property. But it also generates something else: a projected value, which you access by prefixing the property name with a dollar sign ($). By default, the projected value is simply the property wrapper instance itself. While that's occasionally useful, the real power comes when you explicitly define a projectedValue property inside your wrapper struct.
Think of it as a way to provide a different "view" of the same data. While the wrappedValue handles the primary data type, the projectedValue can return a completely different type—like a boolean indicating validity, a publisher for observing changes, or even a closure.
Exposing Metadata via the Projected Value
Let's look at how we could have fixed that registration form. Instead of creating separate booleans in the ViewModel, we can bake the validation state directly into the wrapper's projection.
@propertyWrapper
struct Validated {
private var value: String = ""
private let validation: (String) -> Bool
var wrappedValue: String {
get { value }
set { value = newValue }
}
// This is the magic part.
// Accessing $property will now return this Boolean.
var projectedValue: Bool {
validation(value)
}
init(wrappedValue: String, validation: @escaping (String) -> Bool) {
self.value = wrappedValue
self.validation = validation
}
}
Now, look at how much cleaner the implementation becomes in the actual class. I personally love how this separates the data from the state of the data:
struct RegistrationForm {
@Validated(validation: { $0.contains("@") })
var email: String = ""
}
var form = RegistrationForm()
form.email = "invalid-email"
print(form.email) // "invalid-email" (wrappedValue)
print(form.$email) // false (projectedValue)
form.email = "dev@example.com"
print(form.$email) // true
When you call form.email, you're talking to the wrappedValue. When you call form.$email, you're talking to the projectedValue. It's a clean, elegant way to attach helper logic to a property without adding noise to your model's public API.
📋 Practical Task
Building a Change-Tracking History Wrapper
Your goal is to create a property wrapper called @Tracked. This wrapper should store a value of any type, but its projected value should return an array containing the history of every value that has been assigned to that property (including the initial value).
Requirements:
- The
wrappedValueshould be a generic typeT. - The
projectedValueshould be an array of type[T]. - Every time the
wrappedValueis set, the new value must be appended to the history list.
Test your implementation with the following code:
struct UserProfile {
@Tracked var username: String = "Guest"
}
var profile = UserProfile()
profile.username = "Alice"
profile.username = "Bob"
print(profile.username) // Should print "Bob"
print(profile.$username) // Should print ["Guest", "Alice", "Bob"]
There are no comments for now.