[英]Argument type does not conform to expected type MKAnnotation
I'm using this article as reference https://www.thorntech.com/2016/01/how-to-search-for-location-using-apples-mapkit/ to build my app I try to use matchingItem when searchBarText are equal then append to matchingItems.我使用这篇文章作为参考https://www.thorntech.com/2016/01/how-to-search-for-location-using-apples-mapkit/来构建我的应用程序我尝试在 searchBarText 时使用 matchingItem等于 append 到匹配项。 I currently have a error message that Argument type 'ServiceLocation' does not conform to expected type 'MKAnnotation' which means I need to change the variable type for matchingItem.我目前有一条错误消息,表明参数类型“ServiceLocation”不符合预期的类型“MKAnnotation”,这意味着我需要更改 matchItem 的变量类型。 I am not sure what is the best variable types when you want store ServiceLocation later for using as MapView.当您希望稍后存储 ServiceLocation 以用作 MapView 时,我不确定什么是最佳变量类型。 Any Suggestion?有什么建议吗?
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)
}
}
ServiceLocation.swift服务位置.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?
}
So what the compiler is telling you is that you have an array of MKAnnotation
but you're trying to stuff a ServiceLocation
into it which is an unrelated type.所以编译器告诉你的是你有一个MKAnnotation
数组,但你试图将一个ServiceLocation
填充到其中,这是一个不相关的类型。 As Swift is strongly typed, this is just invalid (thing square peg - round hole situation).由于 Swift 是强类型的,这只是无效的(东西方钉 - 圆孔情况)。
What you need to do is map your ServiceLocation
to MKAnnotation
, eg like so:你需要做的是 map 你的ServiceLocation
到MKAnnotation
,例如像这样:
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
})
}
A few options:几个选项:
You could make ServiceLocation
an annotation, by making it a NSObject
subclass that conforms to MKAnnotation
.您可以通过使其成为符合MKAnnotation
的NSObject
子类来使ServiceLocation
成为注释。 So, first make it a class:因此,首先将其设为 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() } }
Second, add MKAnnotation
protocol conformance with a few computed properties:其次,添加具有一些计算属性的MKAnnotation
协议一致性:
extension ServiceLocation: MKAnnotation { var coordinate: CLLocationCoordinate2D { CLLocationCoordinate2D(latitude: latitude, longitude: longitude) } var title: String? { locationName } var subtitle: String? { "\(numAvailableChargers) chargers" } }
Alternatively, you could create an annotation class that has your original service location struct
as a property:或者,您可以创建一个注释 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() } }
There are other permutations on this idea, but the key is to just make an annotation that has your struct
as a property, and then, rather than adding the ServiceLocation
to the map's annotations, add a ServiceLocationAnnotation
.这个想法还有其他的排列方式,但关键是只创建一个将您的struct
作为属性的注释,然后,而不是将ServiceLocation
添加到地图的注释中,添加一个ServiceLocationAnnotation
。
Obviously, make the subtitle whatever you want, but hopefully this illustrates the idea.显然,你可以随意制作字幕,但希望这能说明这个想法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.