Why do I get EXC_BAD_ACCESS right after instantiation?

萝らか妹 提交于 2019-12-14 02:27:34

问题


I was reading this question and still can't figure out something. I placed breakpoints at AA & BB and saw that first AA happens which means destinationPlacemark gets instantiated. later at line BB I po destinationPlacemark and get error :

expression produced error: error: Execution was interrupted, reason: EXC_BAD_ACCESS (code=1, address=0x10). The process has been returned to the state before expression evaluation.

I read some of the answers found here but they are talking about a released object. How can that object be released as quickly as such?!

import UIKit
import CoreLocation

class ViewController: UIViewController {


var destinationPlacemark = CLPlacemark() //AA

override func viewDidLoad() {
    super.viewDidLoad() //BB
    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 plscement conversion")
        return
    }
    if (placemarks?.count)! > 0 {
        let placemark = placemarks?[0]
        let location = self.destinationPlacemark.location
        let coordinate = location?.coordinate
        print("Latitude: \(coordinate!.latitude), Longitude: \(coordinate!.longitude)")

        self.destinationPlacemark = placemark!
    }
})
}

}

回答1:


as explained in the answer in destinationPlacemark = CLPlacemark() was instantiated but has no location specified. Then location is being set to this nil. I posted the original question. Try chaning let location = self.destinationPlacemark.location to location = placemark.location



来源:https://stackoverflow.com/questions/41840082/why-do-i-get-exc-bad-access-right-after-instantiation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!