How to pass object with NSNotificationCenter
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
NSNotificationCenter (modern NotificationCenter) lets you pass context using the object parameter and/or userInfo dictionary. The right choice depends on whether you need a single sender reference, multiple payload fields, or type-safe wrappers. Most bugs come from inconsistent naming, force-casting, and observer lifecycle issues.
Core Sections
1) Post notification with object
Observer side:
2) Pass structured payload with userInfo
Read payload safely:
3) Best practice: typed notification wrappers
Create name constants to avoid stringly-typed errors.
Use these constants across post/observe calls.
4) Observer lifecycle and leaks
For block observers, store tokens and remove when appropriate.
With selector observers, unregister in deinit when required.
Verification Workflow and Operational Hardening
After implementing the fix, validate with a repeatable workflow rather than ad hoc manual checks. A reliable approach is: reproduce baseline, apply one focused change, then verify both expected behavior and nearby edge cases. This keeps debugging causal and makes reviews easier because every observed improvement is traceable to a specific diff.
A simple validation loop:
For codebases with automated tests, immediately translate the reproduced issue into a regression test. This is the fastest way to prevent recurrence after refactors, dependency upgrades, or runtime migrations.
Edge-case validation is essential. Many failures appear only on boundary inputs such as empty collections, null values, unusual encodings, large payloads, or high concurrency. Build a compact table of edge scenarios with expected outcomes, then run it in local and CI environments. This catches hidden assumptions early and reduces production surprises.
Environment parity also matters. A fix that works locally can fail elsewhere due to version differences, OS behavior, architecture (x86 vs ARM), filesystem semantics, or network policy. Capture runtime metadata alongside results so troubleshooting stays grounded in facts.
Before rollout, define rollback criteria and observability signals. Decide in advance which metrics/logs indicate success or regression, and document the rollback command path for on-call responders. Teams recover faster when fallback steps are predefined instead of improvised during incidents.
Finally, isolate functional fixes from broad refactors. Small, focused commits are easier to review, bisect, and revert safely. If normalization, formatting, or dependency upgrades are required, ship them in separate commits to keep risk controlled and diagnosis straightforward.
Common Pitfalls
- Force-casting
objectoruserInfovalues and crashing on mismatch. - Reusing raw string notification names inconsistently.
- Passing large mutable objects without ownership/lifecycle clarity.
- Forgetting to remove observers in long-lived components.
- Mixing sender object semantics with payload semantics ambiguously.
Summary
To pass objects with NotificationCenter, use object for single sender/reference and userInfo for structured payloads. Standardize names and decode payloads safely to prevent runtime crashes. Correct observer lifecycle management keeps notification code stable and leak-free.
A practical way to keep this solution robust over time is to add one focused regression test and one edge-case test that represent your real production data shape. Re-run those checks whenever dependencies, runtime versions, or infrastructure settings change. This small maintenance habit catches compatibility drift early and prevents recurring incidents that otherwise look like random regressions.
Related reading
- How to pass one SwiftUI View as a variable to another View struct
- How to pass prepareForSegue an object
- How to pass the returned Image of an AsyncImage object to a different view in SwiftUI?
- How to pause / sleep thread or process in Android?
- How to pause / sleep thread or process in Android?
- How to perform Unwind segue programmatically?
- How to pick an image from gallery SD Card for my app?
- How to pinch out in iOS simulator when map view is only a portion of the screen?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.