gpt4 book ai didi

swift - Swift 中的函数错误处理

转载 作者:搜寻专家 更新时间:2023-11-01 06:18:56 25 4
gpt4 key购买 nike

我正在我的 CoreLocation 和基于 MapKit frimeworks 的应用程序中实现本地搜索。我正在关注这个 Tutorial

我收到以下错误:

Cannot convert value of type '(MKLocalSearchResponse!, NSError!) -> ()' to expected argument type 'MKLocalSearchCompletionHandler' (aka '(Optional, Optional) -> ()')

这是我的代码:

import UIKit
import CoreLocation
import MapKit


class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate{
private let LocationManager = CLLocationManager()
let request = MKLocalSearchRequest()
private var previousPoint:CLLocation?
private var totalMovementDistance:CLLocationDistance = 0




@IBOutlet var mapView: MKMapView!


@IBOutlet var searchText: UITextField!
var matchingItems: [MKMapItem] = [MKMapItem]()

@IBAction func textFieldReturn(sender:AnyObject) {
sender.resignFirstResponder()
mapView.removeAnnotations(mapView.annotations)
self.performSearch()

}

@IBOutlet var latitudeLabel: UILabel!
@IBOutlet var longitudeLabel: UILabel!
@IBOutlet var horizontalAccuracy: UILabel!
@IBOutlet var altitudeLabel: UILabel!
@IBOutlet var verticalAccuracyLabel: UILabel!
@IBOutlet var distanceTraveledLabel: UILabel!






override func viewDidLoad() {
super.viewDidLoad()
LocationManager.delegate = self
LocationManager.desiredAccuracy = kCLLocationAccuracyBest
LocationManager.requestWhenInUseAuthorization()



// Do any additional setup after loading the view, typically from a nib.
}



func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
print("Authorization Status Changed to \(status.rawValue)")
switch status {
case .Authorized, .AuthorizedWhenInUse:
LocationManager.startUpdatingLocation()
mapView.showsUserLocation = true

default:
LocationManager.stopUpdatingLocation()
mapView.showsUserLocation = false
}
}

func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
let errorType = error.code == CLError.Denied.rawValue ? "Access Denied": "Error \(error.code)"
let alertController = UIAlertController(title: "Location Manager Error", message: errorType, preferredStyle: .Alert)
let okAction = UIAlertAction(title: "OK", style: .Cancel, handler: {action in})
alertController.addAction(okAction)
presentViewController(alertController, animated: true, completion: nil)
}

func performSearch() {

matchingItems.removeAll()
let request = MKLocalSearchRequest()
request.naturalLanguageQuery = searchText.text
request.region = mapView.region

let search = MKLocalSearch(request: request)

search.startWithCompletionHandler { (localResponse:MKLocalSearchResponse?, error: NSError?) -> Void in


if error != nil {
print("Error occured in search: \(error?.localizedDescription)")
} else if localResponse == 0 {
print("No matches found")
} else {
print("Matches found")

for item in localResponse.mapItems {
print("Name = \(item.name)")
print("Phone = \(item.phoneNumber)")

self.matchingItems.append(item as MKMapItem)
print("Matching items = \(self.matchingItems.count)")

var annotation = MKPointAnnotation()
annotation.coordinate = item.placemark.coordinate
annotation.title = item.name
self.mapView.addAnnotation(annotation)
}
}
}
}


func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let newLocation = (locations as [CLLocation]) [locations.count - 1]

let latitudeString = String(format: "%g\u{00B0}", newLocation.coordinate.latitude)
latitudeLabel.text = latitudeString

let longitudeString = String(format: "%g\u{00B0}", newLocation.coordinate.longitude)
longitudeLabel.text = longitudeString

let horizontalAccuracyString = String(format: "%gm", newLocation.horizontalAccuracy)
horizontalAccuracy.text = horizontalAccuracyString

let altitudeString = String(format: "%gm", newLocation.altitude)
altitudeLabel.text = altitudeString

let verticalAccuracyString = String(format: "%gm", newLocation.verticalAccuracy)
verticalAccuracyLabel.text = verticalAccuracyString

if newLocation.horizontalAccuracy < 0 {
//invalid accuracy
return

}


if newLocation.horizontalAccuracy > 100 ||
newLocation.verticalAccuracy > 50 {
return
}

if previousPoint == nil {
totalMovementDistance = 0
let start = Place(title:"Strating Point", subtitle:"This is where we started", coordinate:newLocation.coordinate)
mapView.addAnnotation(start)
let region = MKCoordinateRegionMakeWithDistance(newLocation.coordinate, 100, 100)
mapView.setRegion(region, animated:true)


}else {
print("movement distance: \(newLocation.distanceFromLocation(previousPoint!))")
totalMovementDistance += newLocation.distanceFromLocation(previousPoint!)

}

previousPoint = newLocation

let distanceString = String(format: "%gm", totalMovementDistance)
distanceTraveledLabel.text = distanceString



}



}

错误在这一行:

search.startWithCompletionHandler({(response:
MKLocalSearchResponse!,

编辑:

enter image description here

最佳答案

替换!在与? (因为那是方法的签名)示例:

    search.startWithCompletionHandler { (localResponse:MKLocalSearchResponse?, error:NSError?) -> Void in

}

关于swift - Swift 中的函数错误处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38112368/

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