【问题标题】:Value of type 'UIViewController' has no member 'mapView'“UIViewController”类型的值没有成员“mapView”
【发布时间】:2018-01-10 23:18:02
【问题描述】:

这是我在 util 文件中的功能。它抱怨“'UIViewController' 类型的值没有成员 'mapView'”

func removeSpecificCustomAnnotation(annotationArray: [CustomPointAnnotation], annotationIdToRemove: String, viewController: UIViewController) {
    for annotation in annotationArray {
        if let ID = annotation.locationID, ID == annotationIdToRemove {
            viewController.mapView.removeAnnotation(annotation)
        }
    }
}

我不能再回答了。我在这里发布我的工作内容

这是工作功能。我想删除地图上的注释

  1. 在我的 viewController.swift 中
  2. 声明 var mapView = MKMapView()
  3. 显示所有注释的功能 -> 我不放这里
  4. 现在,我想删除上面显示的注释之一
  5. 我有注释的 ID(您需要使用自定义注释来执行此操作)
  6. 注解ID由函数根据经纬度生成
  7. 现在,我需要找到具有该 ID 的注释并将其删除
  8. 我得到了地图上的所有注释

    让注解 = mapView.annotations.filter { $0 !== self.mapView.userLocation }

  9. 循环注释数组,然后删除我需要的那个

    for annotation in annotations {
    
        let getLat: CLLocationDegrees = annotation.coordinate.latitude
        let getLon: CLLocationDegrees = annotation.coordinate.longitude
    
        let cllocation: CLLocation =  CLLocation(latitude: getLat, longitude: getLon)
    
        let locationID = Utils.generateLocationID(newcllocation: cllocation, existingCllocationToEdit: nil)
    
        if locationID == annotationIdToRemove {
            self.mapView.removeAnnotation(annotation)
        }
    
    
    }
    

【问题讨论】:

  • 导入框架。
  • @ElTomato 什么框架?这不是框架问题。这是试图访问 UIViewController 上不存在的属性。

标签: ios swift mapkit


【解决方案1】:

错误非常明确地告诉您出了什么问题。您的函数removeSpecificCustomAnnotation() 采用viewController 类型的参数UIViewController。这是一个通用的UIViewController,而UIViewController 对象没有mapView 属性。您必须以某种方式告诉编译器您的视图控制器是某种视图控制器,确实具有mapView 属性。

正如 Farid 在他的回答中所建议的那样,您可以让您的视图控制器成为 UIViewController 的自定义子类。但是,这意味着该函数只能采用该类或子类的视图控制器。

相反,我建议定义一个协议,说明“符合此协议的对象具有MKMapView 类型的变量mapView”:

protocol HasMapView {
  var mapView: MKMapView { get }
}

编辑:

我最初的答案使用了泛型,但 Rob 的答案更好,所以我正在编辑我的答案以采用他的关键部分:

//I don't know what your CustomPointAnnotation class is;
//The below makes the function compile
class CustomPointAnnotation: NSObject {
}

func removeSpecificCustomAnnotation(
  annotationArray: [CustomPointAnnotation],
  annotationIdToRemove: String,
  viewController: UIViewController & HasMapView) {
    print("\(viewController.mapView)")
}

表达式UIViewController & HasMapView 告诉编译器viewController 参数是任何也符合HasMapView 属性的UIViewController,因此它保证有一个mapView 类型的变量MKMapView

编辑#2:

您的参数类型为UIViewController,但在您的代码中没有什么要求它是视图控制器。如果这确实是您的全部功能,您可以删除该要求,并使参数只是符合 HasMapView 协议的某个对象,而不要求它是视图控制器:

func removeSpecificCustomAnnotation(
  annotationArray: [CustomPointAnnotation],
  annotationIdToRemove: String,
  viewController: HasMapView) {
    print("\(viewController.mapView)")
}

【讨论】:

  • 为什么不把HasMapView 作为一个类型呢?为什么要打扰... & UIViewController?他没有做任何需要它成为视图控制器的事情......
  • 我假设有一些其他原因(他的帖子中没有显示)传入的东西需要是一个视图控制器,但是,如果它不是必须的,不需要强制。
【解决方案2】:

假设您有一个自定义的UIViewController 子类,它有一个mapView 属性,并且此函数中viewController 参数的类型是您自定义的UIViewController 子类,您需要将viewController 参数类型更改为您在函数声明中的自定义视图控制器子类名称:

func removeSpecificCustomAnnotation(annotationArray: [CustomPointAnnotation], annotationIdToRemove: String, viewController: YourCustomViewController) {

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    • 2016-03-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多