Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 参数类型不符合预期的类型MKAnnotation_Ios_Swift_Mkmapview - Fatal编程技术网

Ios 参数类型不符合预期的类型MKAnnotation

Ios 参数类型不符合预期的类型MKAnnotation,ios,swift,mkmapview,Ios,Swift,Mkmapview,我用这篇文章作为参考 构建我的应用程序 我尝试在searchBarText相等时使用matchingItem,然后将其附加到matchingItems。 我目前有一条错误消息 参数类型“ServiceLocation”与预期类型“MKAnnotation”不一致 这意味着我需要更改matchingItem的变量类型。 我不确定以后要将StoreServiceLocation用作MapView时,最好的变量类型是什么。 有什么建议吗 var matchingItems = [MKAnnotatio

我用这篇文章作为参考 构建我的应用程序 我尝试在searchBarText相等时使用matchingItem,然后将其附加到matchingItems。 我目前有一条错误消息 参数类型“ServiceLocation”与预期类型“MKAnnotation”不一致 这意味着我需要更改matchingItem的变量类型。 我不确定以后要将StoreServiceLocation用作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)
                    
                }
            }
ServiceLocation.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是强类型的,所以这是无效的(东西是方形的-圆孔的情况)

您需要做的是将您的
服务位置映射到
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
       })
}

因此,编译器告诉您的是,您有一个
MKAnnotation
数组,但您试图将一个
ServiceLocation
填充到其中,这是一个不相关的类型。因为Swift是强类型的,所以这是无效的(东西是方形的-圆孔的情况)

您需要做的是将您的
服务位置映射到
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
       })
}
有几个选择:

  • 您可以将
    ServiceLocation
    设置为注释,方法是将其设置为符合
    MKAnnotation
    NSObject
    子类。所以,首先让它成为一个类:

     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" }
     }
    
  • 或者,您可以创建一个注释类,将原始服务位置
    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

  • 显然,你可以随心所欲地制作字幕,但希望这能说明这个想法。

    有几个选项:

  • 您可以将
    ServiceLocation
    设置为注释,方法是将其设置为符合
    MKAnnotation
    NSObject
    子类。所以,首先让它成为一个类:

     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" }
     }
    
  • 或者,您可以创建一个注释类,将原始服务位置
    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



  • 显然,无论您想要什么,都可以制作字幕,但希望这能说明这一点。

    因此我得到的“MKAnnotation”无法构造,因为它在第行上没有可访问的初始值设定项let annotation=MKAnnotation()抱歉,已经有一段时间了,请参阅更新(更改为
    MKPointAnnotation()
    )谢谢!所以我补充说,这似乎没有错误。我已经有了搜索字幕和标题的文件,并希望附加到匹配中以供以后搜索,并在用户单击时放大到该位置,以便不想返回注释。github.com/williamkwon97/searchMapSwift这是我从ServiceLocationViewController.swift搜索CustomAnnotation的代码,你能告诉我如何修复它吗?所以我用你的代码进行了更新,它的工作方式与以前不同。您知道如何修改LocationSearchTable.swift吗?还有多个位置列表,但我认为这只适用于一个位置[链接](github.com/williamkwon97/searchMapSwift)
    所以我用你的代码更新了它,它的工作方式和以前不一样了
    你能详细解释一下你的意思吗?所以我得到“MKAnnotation”无法构造,因为它的第行上没有可访问的初始值设定项let annotation=MKAnnotation(),抱歉,已经过了一段时间,请参阅更新(更改为
    MKPointAnnotation()
    )谢谢!所以我补充说,这似乎没有错误。我已经有了搜索字幕和标题的文件,并希望附加到匹配中以供以后搜索,并在用户单击时放大到该位置,以便不想返回注释。github.com/williamkwon97/searchMapSwift这是我从ServiceLocationViewController.swift搜索CustomAnnotation的代码,你能告诉我如何修复它吗?所以我用你的代码进行了更新,它的工作方式与以前不同。您知道如何修改LocationSearchTable.swift吗?还有多个位置列表,但我认为这只适用于一个位置[链接](github.com/williamkwon97/searchMapSwift)
    ,因此我用您的代码进行了更新,它的工作方式与以前不同
    您能详细说明您的意思吗?您好,谢谢您的建议!我是个新手,还是有点不知道该把它放在哪里,哈哈。所以我已经有了一个应用程序,可以通过“CustomAnnotation”进行搜索,但我需要使用ServiceLocation title=locationName和subtitle应该是首字母缩略词,并通过两者进行搜索。这是我的github,你能看一下并推一下吗?你的意思是什么?对不起,额外的工作要放在哪里?让我们假设您使用第一个示例,将
    ServiceLocation
    放在
    ServiceLocation.swift
    文件中,您现在拥有它,您只需要在文件开头添加一个
    import MapKit
    。完成了。所以当我添加到扩展ServiceLocation:MKAnnotation时,我使用了未声明的类型“MKAnnotation”和未声明的类型“CLLocationCoordinate2D”:MKAnnotation您是否执行了导入MapKit
    ?嗨,所以我尝试了第二种方法来创建类ServiceLocationAnnotation并基本上创建var allServiceLocations:[ServiceLocationAnnotation]=[]然后替换为matchingItems=sel