CLGeocoder Segmentation fault: 11 swiftUI

Hi, I have an issue in xcode 13,when i implement two CLGeocoder.reverseGeocodeLocation(), i get error that says Segnentation fault: 11, any help? thanks.

the code:

`              CLGeocoder().reverseGeocodeLocation(CLLocation(latitude: self.parent.source.latitude, longitude: self.parent.source.longitude)){ (placemarks, error) in

                    guard error == nil else{

                        print((error?.localizedDescription)!)

                        return

                    }

                    if (placemarks?.count)! > 0 {

                        let pm = placemarks?[0] as! CLPlacemark!

                        self.parent.userLocation = (pm!.subThoroughfare)! + "" + (pm?.thoroughfare)! + "" + (pm?.locality)!

                    }

                }

            }

        }

Answered by OOPer in 692337022

i get error that says Segnentation fault: 11,

Generally, stopping with Segmentation Fault 11 is caused by some bug of Swift compiler.

(You should better send a bug report to bugs.swift.org .)

And in most cases, you can avoid this sort of bugs by fixing some sort of bugs in your code. It depends on many things, but if all your hidden codes are as I expect, it should be something like this:

        CLGeocoder().reverseGeocodeLocation(CLLocation(latitude: self.parent.source.latitude, longitude: self.parent.source.longitude)){ (placemarks, error) in
            if let error = error {
                print(error)
                return
            }
            guard let placemarks = placemarks else {
                print("placemarks is nil")
                return
            }
            if let pm = placemarks.first {
                self.parent.userLocation =
                    (pm.subThoroughfare ?? "") +
                    (pm.thoroughfare ?? "") +
                    (pm.locality ?? "")
            }
        }

(When showing some code, please use the Code Block feature. And better make parentheses balanced.)

Generally, you should better avoid using forced something (forced unwrapping !, forced casting as! and implicitly unwrapped Optional !) as far as you can. Please try the code above.

Oh... you mean you get the error when you build the app... it would have been worth pointing that out!

My first thought is that you are using a lot of force-unwrapping (!), and it would be better to avoid that.

Try:

if let pm = placemarks?.first {
    self.parent.userLocation = (pm.subThoroughfare ?? "") + (pm.thoroughfare ?? "") + (pm.locality ?? "")
}
Accepted Answer

i get error that says Segnentation fault: 11,

Generally, stopping with Segmentation Fault 11 is caused by some bug of Swift compiler.

(You should better send a bug report to bugs.swift.org .)

And in most cases, you can avoid this sort of bugs by fixing some sort of bugs in your code. It depends on many things, but if all your hidden codes are as I expect, it should be something like this:

        CLGeocoder().reverseGeocodeLocation(CLLocation(latitude: self.parent.source.latitude, longitude: self.parent.source.longitude)){ (placemarks, error) in
            if let error = error {
                print(error)
                return
            }
            guard let placemarks = placemarks else {
                print("placemarks is nil")
                return
            }
            if let pm = placemarks.first {
                self.parent.userLocation =
                    (pm.subThoroughfare ?? "") +
                    (pm.thoroughfare ?? "") +
                    (pm.locality ?? "")
            }
        }

(When showing some code, please use the Code Block feature. And better make parentheses balanced.)

Generally, you should better avoid using forced something (forced unwrapping !, forced casting as! and implicitly unwrapped Optional !) as far as you can. Please try the code above.

CLGeocoder Segmentation fault: 11 swiftUI
 
 
Q