Ios 类型为'的值;UIViewController';没有成员';地图视图';
这是我在util文件中的功能。它抱怨“类型'UIViewController'的值没有成员'mapView'” 我不能再回答了。我把我的工作用品贴在这里 这是工作函数。我想删除地图上的注释Ios 类型为'的值;UIViewController';没有成员';地图视图';,ios,swift,mapkit,Ios,Swift,Mapkit,这是我在util文件中的功能。它抱怨“类型'UIViewController'的值没有成员'mapView'” 我不能再回答了。我把我的工作用品贴在这里 这是工作函数。我想删除地图上的注释 在我的viewController.swift中 声明var mapView=MKMapView() 函数显示所有注释->我不放在这里 现在,我想删除上面显示的注释之一 我有注释的ID(您需要使用自定义注释来完成此操作) 注释的ID由基于纬度和经度的函数生成 现在,我需要找到具有该ID的注释并将其删除 我在地
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)
}
}
假设您有一个自定义的
UIViewController
子类,该子类具有mapView
属性,并且此函数中viewController
参数的类型是自定义的UIViewController
子类,您需要在函数声明中将viewController
参数类型更改为自定义视图控制器子类名称:
func removeSpecificCustomAnnotation(annotationArray: [CustomPointAnnotation], annotationIdToRemove: String, viewController: YourCustomViewController) {
}
这个错误非常明确地告诉你出了什么问题。函数
removesspecificcustomannotation()
采用UIViewController
类型的参数viewController
。它是一个通用的UIViewController
,并且UIViewController
对象没有mapView
属性。您必须以某种方式告诉编译器,您的视图控制器是某种视图控制器,确实具有mapView
属性
您可以使视图控制器成为UIViewController的自定义子类,正如Farid在回答中所建议的那样。但是,这意味着该函数只能查看该类或子类的控制器
相反,我建议定义一个协议,说明“符合此协议的对象具有类型为MKMapView
的变量mapView
”:
编辑:
我最初的答案使用了泛型,但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
,因此它保证具有MKMapView
类型的变量mapView
编辑#2:
您的参数是typeUIViewController
,但在代码中没有要求它是视图控制器的要求。如果这真的是您的全部功能,您可以删除该要求,并使参数只是符合HasMapView协议的某个对象,而不要求它是视图控制器:
func removeSpecificCustomAnnotation(
annotationArray: [CustomPointAnnotation],
annotationIdToRemove: String,
viewController: HasMapView) {
print("\(viewController.mapView)")
}
将框架导入到哪个框架?这不是一个框架问题。这是试图访问
UIViewController
上不存在的属性。为什么不将hasmappview
作为一个类型?为什么要费心于…&UIViewController
?他没有做任何需要它成为视图控制器的事情……我假设还有其他原因(他的帖子中没有显示),传入的东西需要是视图控制器,但是是的,如果不需要,就不需要强制它。
//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)")
}
func removeSpecificCustomAnnotation(
annotationArray: [CustomPointAnnotation],
annotationIdToRemove: String,
viewController: HasMapView) {
print("\(viewController.mapView)")
}