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
42: Building a Custom Property Wrapper
A few years ago, I was reviewing a PR from a junior developer who had built a settings screen for a complex productivity app. I noticed their SettingsManager class was absolutely bloated. For every single user preference—theme, notification toggle, sync interval—they had written a manual getter and setter that interfaced with UserDefaults. It was about 100 lines of repetitive boilerplate: get { return UserDefaults.standard.string(forKey: "theme") ?? "light" } and so on. It wasn't "wrong," but it was tedious to read and a nightmare to maintain. If they wanted to change how they stored data, they had to touch twenty different properties.
This is exactly the kind of friction that Property Wrappers were designed to eliminate. Instead of writing the same logic over and over inside your properties, you encapsulate that logic into a separate structure. Once defined, you can apply that logic to any property with a single attribute, like @UserDefault. It effectively separates the storage logic from the business logic of your class.
Designing the WrappedValue Interface
To create a property wrapper, you define a struct or class and mark it with the @propertyWrapper attribute. The only hard requirement is that your type must implement a property called wrappedValue. This is the magic hook; whenever you access the property in your main class, Swift redirects that call to the wrappedValue of your wrapper.
Let's fix that SettingsManager problem. I usually start by making the wrapper generic so it can handle Strings, Bools, or Integers without needing separate wrappers for each type. Here is how I'd build it:
@propertyWrapper
struct UserDefault<T> {
let key: String
let defaultValue: T
var wrappedValue: T {
get {
return UserDefaults.standard.object(forKey: key) as? T ?? defaultValue
}
set {
UserDefaults.standard.set(newValue, forKey: key)
}
}
}
Notice that I'm using a generic T. I personally find it much cleaner to pass the defaultValue into the initializer. That way, the property wrapper doesn't have to guess what a "sane" default is for every different setting in your app.
Applying the Wrapper to Your Models
Once the wrapper is defined, using it is incredibly clean. You no longer need to write explicit getters and setters in your manager class. You just declare the property and tag it. Swift handles the initialization of the UserDefault struct behind the scenes.
class SettingsManager {
@UserDefault(key: "app_theme", defaultValue: "system")
var theme: String
@UserDefault(key: "notifications_enabled", defaultValue: true)
var isNotificationsEnabled: Bool
@UserDefault(key: "sync_interval", defaultValue: 3600)
var syncInterval: Int
}
When you call manager.theme = "dark", Swift isn't actually assigning "dark" to a simple string. It's calling the set block of our UserDefault wrapper, which in turn updates UserDefaults. I love this pattern because if I later decide to move my settings from UserDefaults to a database or a JSON file, I only have to change the code in one place—the wrapper—and the rest of my app remains completely untouched.
One quick tip: remember that property wrappers are initialized when the containing object is created. If your wrapper needs to do something heavy, be mindful of where you're placing these properties. But for most use cases, like validation or persistence, they are an absolute game-changer for keeping your codebase lean.
📋 Practical Task
Building a ClampedNumeric Property Wrapper
In many apps, you have numeric values that must stay within a specific range (for example, a volume slider from 0 to 100, or a zoom level from 1.0 to 5.0). Instead of writing if value > max { value = max } every time you update a variable, you can automate this.
Your Task: Create a property wrapper called @Clamped that takes a minValue and a maxValue. Any value assigned to a property wrapped with @Clamped should be automatically constrained to that range. If a value is set below the minimum, it should be set to the minimum; if it's above the maximum, it should be set to the maximum.
- Define the
@propertyWrapperstructClamped. - Use a generic type
Tthat conforms toComparableandNumeric. - Implement the
wrappedValuelogic to handle the clamping in thesetblock. - Test your wrapper by creating a
PlayerSettingsclass with avolumeproperty clamped between 0 and 100, and try assigning 150 to it to ensure it caps at 100.
There are no comments for now.