为什么我在实例化后立即获得EXC_BAD_ACCESS?

时间:2017-01-24 22:24:42

标签: swift debugging memory-management exc-bad-access

我正在阅读this question,但仍然无法弄明白。我把断点放在AA& BB并看到第一次AA发生,这意味着destinationPlacemark被实例化。后来在BB po destinationPlacemark行并得到错误:

  

表达式产生错误:错误:执行被中断,原因:   EXC_BAD_ACCESS(代码= 1,地址= 0x10)。该过程已被退回   到表达评估前的状态。

我读了一些here找到的答案,但他们正在讨论一个已发布的对象。如何尽快释放该对象?!

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 个答案:

答案 0 :(得分:-1)

the answer in destinationPlacemark = CLPlacemark()中解释的

已实例化但未指定位置。然后将位置设置为此零。我发布了原始问题。尝试将let location = self.destinationPlacemark.location转换为location = placemark.location