1

I want to call a variable (var view) that it is defined in another function (func mapView).

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView?{ if annotation is MapSeismometerAnnotation{ if let annotation = annotation as? MapSeismometerAnnotation{ var view: MKPinAnnotationView view = MKPinAnnotationView(annotation: annotation,reuseIdentifier:annotation.identifier) ... ... } @IBAction func pressPlay(_ sender: Any){ //I want to call here the variable view } 

My class is:

class MapViewController: UIViewController,MKMapViewDelegate,CLLocationManagerDelegate,SCSEarthquakesHandler,SCSPublicSeismometersHandler{ 

If I define the variable var view: MKPinAnnotationView under the class (outside the function), Xcode return me this error:

Cannot override mutable property 'view' of type 'UIView?' with covariant type 'MKPinAnnotationView'

How can I solve this?

2
  • I want to call a variable (var view) please elaborate what is the problem - you can't call a variable inside a function Commented Oct 7, 2020 at 11:03
  • view is a property which is already defined in UIViewController . So you must rename that var to some other name. And if you already declared a property {var view: MKPinAnnotationView} , then again adding var view will be resulting in a new local variable within the method . Commented Oct 7, 2020 at 11:07

3 Answers 3

2

Set another name

var otherName: MKPinAnnotationView 

view variable name conflicts with the vc's UIView property

Sign up to request clarification or add additional context in comments.

Comments

1

View is not overridable because it is used by the UIViewController. Therefore you need to assign another name to the variable than view.

For example: var annotationView: MKAnnotationView is allowed.

Comments

1

view is already defined variable in UIViewController. Pick another name for your MKPinAnnotationView

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.