I'm using CLGeocoder
for reverse geolocation and get array of CLPlacemark
. When I use GPS outside the US (i.e. -27,127) and then access placemark.postalCode
, the app crashes with:
"fatal error: unexpectedly found nil while unwrapping an Optional value".
It seems, that placemark.postalCode
is nil
where no postal code is available. But postalCode
return type in Swift is String!
:
var postalCode: String! { get } // zip code, eg. 95014
So I can't even test is for nil
, because the crash is caused by the getter of postalCode
.
Any ideas how to prevent this crash? Thank you!
Being an optional, even if implicitly unwrapped, you can check it for nil:
and the app won't crash because of that :)
To prove it, just try this code in a playground, where 2 implicitly unwrapped properties (a computed and a stored) are checked for nil: