我在讀this question,仍然無法弄清楚什麼。我在AA & BB處放置了斷點,並看到第一個AA發生,這意味着destinationPlacemark
被實例化。後來在BB線我po destinationPlacemark
,並得到錯誤:爲什麼在實例化後立即獲得EXC_BAD_ACCESS?
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.
我讀一些問題的答案發現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!
}
})
}
}
我認爲,原因是你不應該通過構造函數實例化'CLPlacemark'就像那樣。您只能從地理編碼器獲取它(如示例中所示)。 'CLPlacemark'從Obj-C橋接,它繼承自'NSObject',它提供了零參數初始值設定項,是的,但是它立即返回null對象。 – courteouselk