这是我正在努力将Swift 2转换为Swift 3
的第二部分和最后一部分旧的工作代码是
func calculateSegmentDirections(index: Int,
time: NSTimeInterval, routes: [MKRoute]) {
let request: MKDirectionsRequest = MKDirectionsRequest()
request.source = locationArray[index].mapItem
request.destination = locationArray[index+1].mapItem
request.requestsAlternateRoutes = true
request.transportType = .Automobile
let directions = MKDirections(request: request)
directions.calculateDirectionsWithCompletionHandler ({
(response: MKDirectionsResponse?, error: NSError?) in
if let routeResponse = response?.routes {
let quickestRouteForSegment: MKRoute =
routeResponse.sort({$0.expectedTravelTime <
$1.expectedTravelTime})[0]
var timeVar = time
var routesVar = routes
routesVar.append(quickestRouteForSegment)
timeVar += quickestRouteForSegment.expectedTravelTime
if index+2 < self.locationArray.count {
self.calculateSegmentDirections(index+1, time: timeVar, routes: routesVar)
} else {
self.showRoute(routesVar, time: timeVar)
self.hideActivityIndicator()
}
} else if let _ = error {
let alert = UIAlertController(title: nil,
message: "Directions not available.", preferredStyle: .Alert)
let okButton = UIAlertAction(title: "OK",
style: .Cancel) { (alert) -> Void in
self.navigationController?.popViewControllerAnimated(true)
}
alert.addAction(okButton)
self.presentViewController(alert, animated: true,
completion: nil)
}
})
}
转换后的代码是
func calculateSegmentDirections(index: Int,
time: NSTimeInterval, routes: [MKRoute]) {
let request: MKDirectionsRequest = MKDirectionsRequest()
request.source = locationArray[index].mapItem
request.destination = locationArray[index+1].mapItem
request.requestsAlternateRoutes = true
request.transportType = .Automobile
let directions = MKDirections(request: request)
directions.calculateDirectionsWithCompletionHandler ({
(response: MKDirectionsResponse?, error: NSError?) in
if let routeResponse = response?.routes {
let quickestRouteForSegment: MKRoute =
routeResponse.sort({$0.expectedTravelTime <
$1.expectedTravelTime})[0]
var timeVar = time
var routesVar = routes
routesVar.append(quickestRouteForSegment)
timeVar += quickestRouteForSegment.expectedTravelTime
if index+2 < self.locationArray.count {
self.calculateSegmentDirections(index+1, time: timeVar, routes: routesVar)
} else {
self.showRoute(routesVar, time: timeVar)
self.hideActivityIndicator()
}
} else if let _ = error {
let alert = UIAlertController(title: nil,
message: "Directions not available.", preferredStyle: .Alert)
let okButton = UIAlertAction(title: "OK",
style: .Cancel) { (alert) -> Void in
self.navigationController?.popViewControllerAnimated(true)
}
alert.addAction(okButton)
self.presentViewController(alert, animated: true,
completion: nil)
}
})
}
它在行上抛出错误
directions.calculateDirectionsWithCompletionHandler ({
错误是
无法转换类型的值'(MKDirectionsResponse?,NSError?) - &gt; ()'到期望的参数类型'MKDirectionsHandler'(又名'(可选,可选) - &gt;()')
如果有人能帮助我,我会非常感激!!
答案 0 :(得分:4)
NSError
重命名为Error
这可能会解决您的问题。
这段代码为我编译:
func calculateSegmentDirections(index: Int,
time: TimeInterval, routes: [MKRoute]) {
let request: MKDirectionsRequest = MKDirectionsRequest()
request.source = locationArray[index].mapItem
request.destination = locationArray[index+1].mapItem
request.requestsAlternateRoutes = true
request.transportType = .automobile
let directions = MKDirections(request: request)
directions.calculate (completionHandler: {
(response: MKDirectionsResponse?, error: Error?) in
if let routeResponse = response?.routes {
let quickestRouteForSegment: MKRoute =
routeResponse.sorted(by: {$0.expectedTravelTime <
$1.expectedTravelTime})[0]
var timeVar = time
var routesVar = routes
routesVar.append(quickestRouteForSegment)
timeVar += quickestRouteForSegment.expectedTravelTime
if index+2 < self.locationArray.count {
self.calculateSegmentDirections(index+1, time: timeVar, routes: routesVar)
} else {
self.showRoute(routesVar, time: timeVar)
self.hideActivityIndicator()
}
} else
if let _ = error {
let alert = UIAlertController(title: nil,
message: "Directions not available.", preferredStyle: .alert)
let okButton = UIAlertAction(title: "OK",
style: .Cancel) { (alert) -> Void in
self.navigationController?.popViewControllerAnimated(true)
}
alert.addAction(okButton)
self.presentViewController(alert, animated: true,
completion: nil)
}
})
}