Because force casting (as!
) does not perform any type safety validations, it is capable of performing dangerous conversions between
unrelated types. When the types are truly unrelated, the cast will cause a system crash.
Noncompliant Code Example
foo as! MyClass // Noncompliant
Compliant Solution
foo as? MyClass
See
- CppCoreGuidelines, Type safety profile - Type.1: Don’t use reinterpret_cast.