CLPlacemark crash in Swift
Being an optional, even if implicitly unwrapped, you can check it for nil:
if placemark.postalCode != 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:
struct Test {
var nilComputed: String! { return nil }
var nilStored: String! = nil
}
var test = Test()
if test.nilComputed != nil {
print("It's not nil")
} else {
print("It's nil")
}
if test.nilStored != nil {
print("It's not nil")
} else {
print("It's nil")
}
EXC_BAD_ACCESS when extending CLPlacemark -- variable accessed outside definition scope
I recently run into this issue when testing feature which required providing predefined CLPlacemark
instances as an input. After some research I found that someone had similar problem with CLBeacon
class.
EXC_BAD_ACCESS when setting a CLBeacon to nil
Placemark objects are typically generated by a CLGeocoder object, although you can also create them explicitly yourself.
Despite Apple documentation saying that you can create CLPlacemark
instances by yourself. This class is not a nice guy when it comes to subclassing. It depends on a private class called CLPlacemarkInternal
which is nil
when you create an instance. On the image below you can see how this object looks in debugger. The _internal
ivar has value of 0x0
which is nil
.
Crash with EXC_BAD_ACCESS
message occurs when the object you instantiated gets deallocated. Regardless whether you go out of scope or assign another object (or nil
) to the variable. Why is it happening? This is a question to Apple developers. But below you can find some workarounds other people implemented.
- http://szulctomasz.com/2015/07/01/ios-unit-testing-in-swift-and-clplacemark-mocking.html
- Extending CLPlacemark results in EXC BAD ACCESS
iOS application crashes when trying to get device location - Thread 1: EXC_BAD_ACCESS
but i don't know which fix this,
Instead of calling the extension function as
print(CLPlacemark().toString(placemark))
call it as like this,
print(placemark.toString(placemark))
because there is a memory leakage problem. Now, Its not crashing.
Crash in Geocode call
*Updated with cause *
You had quite a few things here, so I will try to step through the changes I made to fix it:
First, why it crashes:
You create your destinationPlacemark with the following command:
var destinationPlacemark = CLPlacemark()
So when this runs:
let location = self.destinationPlacemark.location
It is running against an empty placemark that contains no location data. By pulling the location from the retrieved placemark created in the guard statement, this issue is avoided.
Next, the working code:
import UIKit
import CoreLocation
class ViewController: UIViewController {
var destinationPlacemark: CLPlacemark?
override func viewDidLoad() {
super.viewDidLoad()
forwardGeocoding(address: "1 Infinite Loop, Cupertino, CA 95014")
}
func forwardGeocoding(address: String) {
CLGeocoder().geocodeAddressString(address, completionHandler: { (placemarks, error) in
if error != nil {
print(error ?? "Error in pla conversion")
return
}
guard let placemark = placemarks?.first, let coordinate = placemark.location?.coordinate else { return }
print("Latitude: \(coordinate.latitude), Longitude: \(coordinate.longitude)")
self.destinationPlacemark = placemark
})
}
}
Which produces:
Latitude: 37.3316941, Longitude: -122.030127
- You do not set up the variable for
destinationPlacemark
properly, see the above for a proper setup - Your address for Apple is wrong, I added a 1 to the front, though you still get a value
- You are forcing too many items, which leads to crashes as you saw. I replaced your forced casts with a guard statement to prevent those
- I de-clutttered some variables in your success section to streamline the code
Why does CLGeocoder crash upon call
You need to init CLGeocoder
.
CLGeocoder *geoCoder = [[CLGeocoder alloc] init];
[geoCoder geocodeAddressString:@"1 Infinite Loop, Cupertino, CA 95014"
completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
if(error){
NSLog(@"%@", error);
}
if(placemarks){
NSLog(@"%@", placemarks);
}
}];
CLLocationManager make my app crash having location activated
You should chek if
self.locationManager.location
Is null before using it
CLLocationManager - fatal error: unexpectedly found nil while unwrapping an Optional value
I add the variables in class, and fix my IBAction
var locality: String = ""
var postalCode: String = ""
var administrativeArea: String = ""
var country: String = ""
@IBAction func addPlace(sender: AnyObject) {
let locality = self.locality
let postalCode = self.postalCode
let administrativeArea = self.administrativeArea
let country = self.country
print(locality + postalCode + administrativeArea + country)
}
Related Topics
Determine The Maximum Number of Characters a UIlabel Can Take
How to Access All Images in a .Xcassets at Once
Detect Permission of Media Library iOS
Using '!' Here Is Deprecated and Will Be Removed in a Future Release - Swift 4.2
Apn Custom Notification Sound Issue
How to Use Sketch UI Elements Directly into Xcode
Launching App from Sms Link or Email Link
Nsdateformatter Still Parsing Instead Having Incorrect Format
Activity Indicator in iOS Launch Screen Doesn't Animate
Why Wkwebview Doesn't Display Videos? - Swift 3
Uidocumentinteractioncontroller Does Not Open Other App in iOS 11
Retrieve All Child Value to a Label in Table View Cell
Bluetooth Didconnect or Didfailtoconnect Not Called Xcode 9.2 with iOS 11.1.2