- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
问题是completionHandler block 中的代码永远不会运行;我使用了断点,程序会在构建模式下跳过完成处理程序 block
下面是 PinALandPlaceMark 中使用的两个函数,我的大部分代码都位于
func generateRandomWorldLatitude()-> Double{
let latitude = Double.random(in: -33 ..< 60)
return latitude
}
func generateRandomWorldLongitude()-> Double{
let longitude = Double.random(in: -180 ..< 180)
return longitude
}
func PinALandPlaceMark() -> MKAnnotation {
var LandBasedCountryHasYetToBeFound : (Bool, CLLocationDegrees?, CLLocationDegrees?)
LandBasedCountryHasYetToBeFound = (false,nil,nil)
let randomPinLocation = MKPointAnnotation()
repeat{
if LandBasedCountryHasYetToBeFound == (false,nil,nil){
let latitude: CLLocationDegrees = generateRandomWorldLatitude()
let longitude: CLLocationDegrees = generateRandomWorldLongitude()
let randomCoordinate = CLLocation(latitude: latitude, longitude: longitude)
let geocoder = CLGeocoder()
geocoder.reverseGeocodeLocation(randomCoordinate, completionHandler: { (placemarks, error) -> Void in
if error != nil{print(error)}else{
guard let placemark = placemarks?.first else{return}
//check which placemark property exists, store in the 0 alpha labelForClosure
if let countryExists = placemark.country {
LandBasedCountryHasYetToBeFound = (true,latitude,longitude)
//country = countryExists as String
// viewController.labelForClosure.text = countryExists
print(" Country Exists!: \(countryExists)")
print(" randomCoordinate \(randomCoordinate)")
}
}
})
}
// print("The country found was on land. This statement is \(LandBasedCountryHasYetToBeFound.occursInCountry)")
else{
let coordinatesOfrandomPinLocation = CLLocationCoordinate2D(latitude: LandBasedCountryHasYetToBeFound.1!, longitude: LandBasedCountryHasYetToBeFound.2!)
randomPinLocation.title = ""//LandBasedCountryHasYetToBeFound.countryName
randomPinLocation.coordinate = coordinatesOfrandomPinLocation
// viewController.mapView.addAnnotation(randomPinLocation)
}
}while LandBasedCountryHasYetToBeFound.0 == false
print("randomPin has been returned, now use pin function inside placemark declaration")
return randomPinLocation
}
最佳答案
您的主要问题是您的 CLGeocoder
实例保存在循环内的局部变量中;这意味着它将在完成任务之前被释放。
即使反向地理编码完成,您还有其他一些问题也会给您带来问题。
主要的是您正在使用设置在闭包内的 bool 值检查循环终止;闭包将异步执行,因此在找到地址的情况下,在 bool 值设置为 true 之前,循环将执行很多次。
第二个问题与此有关并因此变得更糟;反向地理编码受速率限制。如果您过快提交太多请求,Apple 的服务器将简单地返回一个错误。即使您确实在提交第二个响应之前等待了第一个响应,您随机命中土地的机会也非常低,因此您可能会很快达到此限制。
暂时忽略速率限制问题,您可以使用接受完成处理程序的递归函数,而不是使用循环并尝试 return
一个值。
var geoCoder = CLGeocoder()
func pinALandPlaceMark(completion: @escaping (Result<MKAnnotation, Error>) -> Void) {
let latitude: CLLocationDegrees = generateRandomWorldLatitude()
let longitude: CLLocationDegrees = generateRandomWorldLongitude()
let randomCoordinate = CLLocation(latitude: latitude, longitude: longitude)
geocoder.reverseGeocodeLocation(randomCoordinate) { (placemarks, error) in
guard error == nil else {
completion(nil,error)
return error
}
if let placemark = placemarks.first, let _ = placemark.country {
let randomPinLocation = MKPointAnnotation()
randomPinLocation.coordinate = randomCoordinate.coordinate
completionHandler(randomPinLocation,nil)
} else {
pinALandPlaceMark(completion:completion)
}
}
}
CLGeocoder
实例,使其不被释放。
pinALandPlaceMark() { result in
switch result {
case .success(let placemark):
print("Found \(placemark)")
case .failure(let error):
print("An error occurred: \(error)")
}
}
关于ios - 尝试使用 reverseGeocodeLocation,但未执行 completionHandler 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59914591/
当我在 Debug模式下在 iPhone 模拟器中进行身份验证时,将运行下面代码中的第一个 if 语句。但是,当我在安装了 Evernote 客户端的 iPhone 上进行调试时,if 语句未被评估。
在我使用 URLSession.dataTask(with:completionHandler:) 创建新的 session 数据任务之后并通过调用其 resume() 来启动任务方法,鉴于应用程序在
我是今天扩展的新手,我收到了这个警告,有人知道如何匹配可选要求吗? Instance method 'widgetPerformUpdate(completionHandler:)' nearly m
我想了解 NSObject 可能属于哪个类(除 NSAlert、NSOpenPanel 和 NSSavePanel 之外),以便响应上述调用(如问题标题中所示)。这是我需要做的场景。菜单选择的操作必须
我在互联网上到处搜索,但无法真正处理我找到的答案。因此,如果有人可以在这里帮助我,我将不胜感激。 我写了一个看起来像这样的函数: func setImage(imageName: String, co
为什么我的完成处理程序出现问题,我该如何解决? func loadImageusingCacheWithUrlString(urlString: String) { self.image =
我试图将 bool 值传递给我的完成处理程序。当应用程序运行时,将 bool 传递给我的完成处理程序的结果决定了我显示的警报。如果我在错误检查中调用它,应用程序会崩溃/锁定。如果我在其他任何地方调用它
所以我试图让它在完成处理程序之后做一些事情,但我一直收到错误: expected CLGeocodeCompletionHandler class func didCompleteGeocoding(
我想为我的 completionHandler 默认参数传递: func firstFunc(completionHandler: ((array:[String:AnyObject] = [:])
我最近加入了一个新项目并开始研究 iOS 应用程序代码库。但是,在最新的 Xcode 10 中,代码不再编译。 protocol NetworkClientType { associatedtyp
以下代码是线程安全的吗?如果是这样,如何保证ByteBuffer 的安全发布?执行 CompletionHandler 的线程的实例? AsynchronousSocketChannel channe
This question already has answers here: Swift 3 URLSession.shared() Ambiguous reference to member 'd
在 Xcode 5.0.2 中设置要显示为模式表的 NSAlert 对象时,我遇到了一个有趣的惊喜。 我计划使用 beginSheetModalForWindow:modalDelegate:didE
我已经执行了代码,但收到completionHandler 错误 如果没有更多上下文,表达类型不明确 据我了解,这是由于completionHandler 不匹配造成的。如何为completionHa
我正在使用 AVAssetImageGenerator 从影片剪辑中获取图像,而无需先播放它。现在我有一个问题如何在处理程序的循环中设置变量?是否可以?我收到此错误消息,但不知道这意味着什么。 (谷歌
我已经执行了代码,但收到completionHandler 错误 如果没有更多上下文,表达类型不明确 据我了解,这是由于completionHandler 不匹配造成的。如何为completionHa
我试图在加载数据后加载我的 CollectionView,但我不知道应该如何使用 CompletionHandler 来执行此操作。感谢帮助我的代码如下:用于获取数据: func GETData(){
我正在尝试使用 openActiveSessionWithPublishPermissions 在 Facebook 上发布消息,因此如果用户未登录,他需要先登录,然后使用 io6 Facebook
我有以下方法,如果 SessionStatus 完成,我想返回 movieDestinationUrl,但是我不断收到此错误 Unexpected non-void return value in v
我有一个 UITextField,它允许用户为 Facebook 照片输入自定义相册名称。 textFieldShouldReturn 后,我将 textField.text 发送到 Facebook
我是一名优秀的程序员,十分优秀!