编译代码时,我在这一行上收到错误:我使用的是Swift 4.0
} as! MKDirectionsHandler)
错误说:Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
有人可以帮忙。代码如下:
@objc func getDirections(sender: AnyObject){
if let selectedPin = selectedPin {
let mapItem = MKMapItem(placemark: selectedPin)
//let launchOptions = [MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving]
//mapItem.openInMaps(launchOptions: launchOptions)
let request = MKDirectionsRequest()
request.source = MKMapItem.forCurrentLocation()
request.destination = destination
request.requestsAlternateRoutes = false
let directions = MKDirections(request: request)
directions.calculate(completionHandler: {(response: MKDirectionsResponse!, error: NSError!) in
if error != nil {
print("Error \(error)")
} else {
//self.displayRout(response)
var overlays = self.mapView.overlays
for route in response.routes {
self.mapView.add(route.polyline, level: MKOverlayLevel.aboveRoads)
for next in route.steps {
print(next.instructions)
}
}
}
} as! MKDirectionsHandler)
}
//***********
func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer!
{
let draw = MKPolylineRenderer(overlay: overlay)
draw.strokeColor = UIColor.purple
draw.lineWidth = 3.0
return draw
}
}
这也是我得到的错误:
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
* frame #0: 0x00000001039d5c74 Map`ViewController.getDirections(sender=0x00007fff5c22e610, self=0x00007ff278553bc0) at ViewController.swift:117
frame #1: 0x00000001039d6828 Map`@objc ViewController.getDirections(sender:) at ViewController.swift:0
frame #2: 0x00000001050d1275 UIKit`-[UIApplication sendAction:to:from:forEvent:] + 83
frame #3: 0x000000010524e4a2 UIKit`-[UIControl sendAction:to:forEvent:] + 67
frame #4: 0x000000010524e7bf UIKit`-[UIControl _sendActionsForEvents:withEvent:] + 450
frame #5: 0x000000010524d6ec UIKit`-[UIControl touchesEnded:withEvent:] + 618
frame #6: 0x00000001056ba1a5 UIKit`_UIGestureEnvironmentSortAndSendDelayedTouches + 5560
frame #7: 0x00000001056b4045 UIKit`_UIGestureEnvironmentUpdate + 1506
frame #8: 0x000000010a912d37 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
frame #9: 0x000000010a912c8e CoreFoundation`__CFRunLoopDoObservers + 430
frame #10: 0x000000010a8f7254 CoreFoundation`__CFRunLoopRun + 1572
frame #11: 0x000000010a8f69b9 CoreFoundation`CFRunLoopRunSpecific + 409
frame #12: 0x0000000107e309c6 GraphicsServices`GSEventRunModal + 62
frame #13: 0x00000001050cf5e8 UIKit`UIApplicationMain + 159
frame #14: 0x00000001039dac77 Map`main at AppDelegate.swift:12
frame #15: 0x000000010d79cd81 libdyld.dylib`start + 1
frame #16: 0x000000010d79cd81 libdyld.dylib`start + 1
答案 0 :(得分:1)
您的代码中存在多个语法问题。
更改此行:
directions.calculate(completionHandler: {(response: MKDirectionsResponse!, error: NSError!) in
为:
directions.calculate(completionHandler: {(response, error) in
请注意error
是Error
,而不是NSError
。
然后摆脱as! MKDirectionsHandler
。没有理由投出完成处理程序。
directions.calculate(completionHandler: { (response, error) in
if error != nil {
print("Error \(error)")
} else {
//self.displayRout(response)
var overlays = self.mapView.overlays
for route in response.routes {
self.mapView.add(route.polyline, level: MKOverlayLevel.aboveRoads)
for next in route.steps {
print(next.instructions)
}
}
}
})