簡體   English   中英

參數類型不符合預期的類型 MKAnnotation

[英]Argument type does not conform to expected type MKAnnotation

我使用這篇文章作為參考https://www.thorntech.com/2016/01/how-to-search-for-location-using-apples-mapkit/來構建我的應用程序我嘗試在 searchBarText 時使用 matchingItem等於 append 到匹配項。 我目前有一條錯誤消息,表明參數類型“ServiceLocation”不符合預期的類型“MKAnnotation”,這意味着我需要更改 matchItem 的變量類型。 當您希望稍后存儲 ServiceLocation 以用作 MapView 時,我不確定什么是最佳變量類型。 有什么建議嗎?

var matchingItems = [MKAnnotation]()
        var handleMapSearchDelegate:HandleMapSearch? = nil
    var allServiceLocations : [ServiceLocation] = []
        
        func updateSearchResults(for searchController: UISearchController) {
            
           
            matchingItems = []
            guard let mapView = mapView,
                let searchBarText = searchController.searchBar.text else { return }
            for location in allServiceLocations{
                if(location.locationName == searchBarText){
                   matchingItems.append(location)
                    
                }
            }

服務位置.swift

struct ServiceLocation: Codable {
    var id: Int
    var locationType : LocationType
    var locationName: String
    var latitude: Double
    var longitude: Double
    var active: Bool
    var home : Bool
    var numAvailableChargers : Int
    var acronym : String?
    var eta: Int?
}

所以編譯器告訴你的是你有一個MKAnnotation數組,但你試圖將一個ServiceLocation填充到其中,這是一個不相關的類型。 由於 Swift 是強類型的,這只是無效的(東西方釘 - 圓孔情況)。

你需要做的是 map 你的ServiceLocationMKAnnotation ,例如像這樣:

var matchingItems = [MKAnnotation]()
var handleMapSearchDelegate:HandleMapSearch? = nil
var allServiceLocations : [ServiceLocation] = []
        
func updateSearchResults(for searchController: UISearchController) {
      guard let mapView = mapView,
            let searchBarText = searchController.searchBar.text else { return }

        matchingItems = allServiceLocations.filter({ $0.locationName == searchBarText })
        .map({ 
            let annotation = MKPointAnnotation()
            annotation.coordinate = CLLocationCoordinate2D(latitude: CLLocationDegrees(exactly: $0.latitude)!, longitude: CLLocationDegrees(exactly: $0.longitude)!)
            annotation.title = $0.locationName
            annotation.subtitle = $0.locationType
        return annotation
       })
}

幾個選項:

  1. 您可以通過使其成為符合MKAnnotationNSObject子類來使ServiceLocation成為注釋。 因此,首先將其設為 class:

     class ServiceLocation: NSObject, Codable { var id: Int var locationType: LocationType var locationName: String var latitude: Double var longitude: Double var active: Bool var home: Bool var numAvailableChargers: Int var acronym: String? var eta: Int? init(id: Int, locationType: LocationType, locationName: String, latitude: Double, longitude: Double, active: Bool, home: Bool, numAvailableChargers: Int, acronym: String?, eta: Int?) { self.id = id self.locationType = locationType self.locationName = locationName self.latitude = latitude self.longitude = longitude self.active = active self.home = home self.numAvailableChargers = numAvailableChargers self.acronym = acronym self.eta = eta super.init() } }

    其次,添加具有一些計算屬性的MKAnnotation協議一致性:

     extension ServiceLocation: MKAnnotation { var coordinate: CLLocationCoordinate2D { CLLocationCoordinate2D(latitude: latitude, longitude: longitude) } var title: String? { locationName } var subtitle: String? { "\(numAvailableChargers) chargers" } }
  2. 或者,您可以創建一個注釋 class ,將您的原始服務位置struct作為屬性:

     class ServiceLocationAnnotation: NSObject, MKAnnotation { var coordinate: CLLocationCoordinate2D { CLLocationCoordinate2D(latitude: serviceLocation.latitude, longitude: serviceLocation.longitude) } var title: String? { serviceLocation.locationName } var subtitle: String? { "\(serviceLocation.numAvailableChargers) chargers" } let serviceLocation: ServiceLocation init(serviceLocation: ServiceLocation) { self.serviceLocation = serviceLocation super.init() } }

    這個想法還有其他的排列方式,但關鍵是只創建一個將您的struct作為屬性的注釋,然后,而不是將ServiceLocation添加到地圖的注釋中,添加一個ServiceLocationAnnotation

顯然,你可以隨意制作字幕,但希望這能說明這個想法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM