Always add appropriate safety checks before operations that could result in undefined behavior, null references, or memory issues. This includes using weak references to prevent retain cycles, verifying exact object types before type-specific operations, and adding conditional checks before calling methods that may return undefined values.

Examples of safety checks to implement:

These patterns prevent crashes, memory leaks, and unexpected behavior by ensuring operations are only performed when it’s safe to do so.