The Apple documentation TN2151 says this for a possible cause of a EXC_BREAKPOINT
/ SIGTRAP
:
a non-optional type with a nil value
But code such as this won't compile:
var x = "hello"
x = nil
So under what circumstances can a non-optional obtain a nil
value?
Consider something like this in Objective-C bridged to Swift:
- (NSObject * _Nonnull)someObject {
return nil;
}
The function is annotated as _Nonnull
, but will return nil. This bridged as a non-optional object to Swift and will crash.
Contrived, but possible:
class C {}
let x: C? = nil
let y: C = unsafeBitCast(x, to: C.self)
print(y) // boom
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With