gpt4 book ai didi

ios - addressDictionary 随机返回 nil

转载 作者:行者123 更新时间:2023-11-28 12:57:54 26 4
gpt4 key购买 nike

我用过 this code对于我的应用程序。它运行良好,但有时会在跟踪我的路线 +/- 50 秒后崩溃。我知道它与可选项“?”有关,但我无法让它工作。

我收到以下消息:

fatal error: unexpectedly found nil while unwrapping an Optional

代码中断的部分:

 if let locationName = placeMark.addressDictionary?["Name"] as? NSString
{
print(locationName)
self.locationName = locationName as String
}

完整的 locationManager 代码:

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

for location in locations as [CLLocation] {
let howRecent = location.timestamp.timeIntervalSinceNow

//start motion tracker
motionTracker()

//location name tracker
let locValue:CLLocationCoordinate2D = manager.location!.coordinate
let latitude: CLLocationDegrees = locValue.latitude
let longitude: CLLocationDegrees = locValue.longitude
let geoCoder = CLGeocoder()
let location = CLLocation(latitude: latitude, longitude: longitude)
geoCoder.reverseGeocodeLocation(location)
{
(placemarks, error) -> Void in

let placeArray = placemarks as [CLPlacemark]!

// Place details
var placeMark: CLPlacemark!
placeMark = placeArray?[0]

// Address dictionary
print(placeMark.addressDictionary)

// Location name
if let locationName = placeMark.addressDictionary?["Name"] as? NSString
{
print(locationName)
self.locationName = locationName as String
}

// Street address
if let street = placeMark.addressDictionary?["Thoroughfare"] as? NSString
{
//print(street)
self.locationStreet = street as String
}

// City
if let city = placeMark.addressDictionary?["City"] as? NSString
{
self.locationCity = city as String
//print(city)
}

// Zip code
if let zip = placeMark.addressDictionary?["ZIP"] as? NSString
{
//print(zip)
}

// Country
if let country = placeMark.addressDictionary?["Country"] as? NSString
{
//print(country)
}
}

最佳答案

我怀疑您遇到了这样一种情况,即 Google 没有您所在位置的反向地理编码结果并且返回一个空数组。

代替:

let placeArray = placemarks as [CLPlacemark]!
var placemark: CLPlacemark!
placemark = placeArray?[0]

…假设会有一个数组并且它总是包含至少一个元素,使用:

if let placemark = placemarks?.first {
// Rest of your code
}

关于ios - addressDictionary 随机返回 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34564869/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com