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
73: NotificationCenter for Event Broadcasting
Listen, I see this all the time when developers first encounter NotificationCenter: they start treating it like a "magic glue" that can replace delegates, closures, and direct references. They think, "Why bother passing a delegate from the View Controller to the Service class when I can just fire a notification from anywhere and have anyone listen for it?"
"It's just a more flexible Delegate pattern"
If you use NotificationCenter as a replacement for one-to-one communication, you're setting a trap for yourself. Imagine you have a PaymentProcessor class and a CheckoutViewController. If the processor sends a .paymentDidComplete notification, and the checkout view listens for it, it looks clean at first. But as your app grows, you'll find yourself staring at a bug where a payment completes, but you have no idea who is actually handling the response or why three different unrelated classes are suddenly triggering side effects. You've traded a clear, traceable line of communication for a "shout into the void" architecture. It makes debugging a nightmare because you can't just Command-Click on a method to see who is calling it.
Broadcasting to a Crowd vs. Talking to a Person
The correct mental model is broadcasting. Use NotificationCenter when one event needs to notify an unknown number of observers who don't necessarily have a direct relationship with the sender.
Think of a music app. When a song starts playing, several unrelated parts of the UI need to react: the "Now Playing" bar at the bottom of the screen needs to animate, the "Playlist" view needs to highlight the current track, and perhaps a "Lyrics" view needs to start scrolling. The AudioEngine shouldn't need a reference to three different view controllers just to tell them the song changed. It just shouts, "Hey, the song changed!" and whoever cares handles it.
// First, always extend Notification.Name to avoid typos and "stringly-typed" code
extension Notification.Name {
static let didChangeSong = Notification.Name("didChangeSong")
}
class AudioEngine {
func playSong(_ song: Song) {
// ... logic to play music ...
// We broadcast the event and pass the song object in the userInfo dictionary
NotificationCenter.default.post(
name: .didChangeSong,
object: nil,
userInfo: ["song": song]
)
}
}
Wiring Up Your Observers
Now, on the receiving end, you need to tell the system that you're listening. I usually recommend doing this in viewDidLoad or an initializer. You provide a selector—a function that will execute whenever that specific notification name is broadcast.
class NowPlayingBar: UIView {
init() {
super.init(frame: .zero)
NotificationCenter.default.addObserver(
self,
selector: #selector(handleSongChange(_:)),
name: .didChangeSong,
object: nil
)
}
@objc func handleSongChange(_ notification: Notification) {
// Extract the data from the userInfo dictionary
if let song = notification.userInfo?["song"] as? Song {
print("Updating UI for song: \(song.title)")
}
}
required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") }
}
Cleaning Up the Noise
One thing that trips people up—especially in older versions of Swift/iOS—is memory leaks. While modern iOS handles some of the cleanup for you, it is still a professional best practice to remove your observers when the object is being destroyed. If you don't, you might end up with "ghost" observers trying to call methods on objects that no longer exist, leading to crashes or weird behavior.
I always add this to the deinit of any class that registers for notifications:
deinit {
NotificationCenter.default.removeObserver(self)
}📋 Practical Task
Implementing a Global Theme Switcher with NotificationCenter
Your goal is to create a system where a user can toggle between "Light Mode" and "Dark Mode," and multiple unrelated views update their appearance instantly.
Requirements:
- Create a
Notification.Nameextension calledthemeDidChange. - Create a
ThemeManagerclass with a functiontoggleTheme(). This function should flip a booleanisDarkModeand post thethemeDidChangenotification, passing the current theme state (true/false) in theuserInfodictionary. - Create two different classes (e.g.,
HeaderViewandSettingsView) that both observethemeDidChange. - In the observer method of both views, print a message to the console:
"[ClassName] switched to [Dark/Light] mode". - Ensure both views properly remove themselves as observers in their
deinitmethods.
There are no comments for now.