2
votes

this is my fuction in util file. it complains about "Value of type 'UIViewController' has no member 'mapView'"

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

i am not allowed to answer anymore. i post my working things here

this is working function. I want to remove a annotation on the map

  1. in my viewController.swift
  2. declare var mapView = MKMapView()
  3. function to display all annotations -> i dont put here
  4. now, i want to remove one of annotation that displayed above
  5. i have the ID of annotation (you need to use a custom annotation to do this)
  6. the ID of annotation is generated by a function based on the latitude and longitude
  7. now, i need to find the annotation that has that ID and remove it
  8. i get all annotations on the map

    let annotations = mapView.annotations.filter { $0 !== self.mapView.userLocation }

  9. loop the annotation arrays and then remove that one i need

    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)
        }
    
    
    }
    
2
Import the framework. - El Tomato
@ElTomato What framework? This isn't a framework issue. It's an attempt to access a non-existent property on UIViewController. - rmaddy

2 Answers

1
votes

The error tells you pretty specifically what's wrong. Your function removeSpecificCustomAnnotation() takes a parameter viewController of type UIViewController. It's a generic UIViewController, and UIViewController objects don't have a mapView property. You somehow have to tell the compiler that your view controller is some sort of view controller that does have a mapView property.

You could make your view controller a custom subclass of UIViewController, as suggested by Farid in his answer. However, that means that this function can only take view controllers of that class or a subclass.

Instead, I suggest defining a protocol that says "objects that conform to this protocol have a variable mapView of type MKMapView":

protocol HasMapView {
  var mapView: MKMapView { get }
}

EDIT:

My original answer used generics, but Rob's answer is better, so I'm editing my answer to adopt the key part of his:

//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)")
}

The expression UIViewController & HasMapView tells the compiler that the viewController parameter is any UIViewController that also conforms to the HasMapView property, and that it is therefore guaranteed to have a variable mapView of type MKMapView.

Edit #2:

You had your parameter as type UIViewController, but in your code there's nothing that requires that it be a view controller. If that really is all of your function, you could remove that requirement, and make the parameter be just some object that conforms to the HasMapView protocol and not require that it be a view controller:

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

Assuming you have a custom UIViewController subclass which has a mapView property, and the type of viewController argument in this function is your custom UIViewController subclass, you need to change the viewController argument type to your custom view controller subclass name in the function declaration:

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

}