I know that this error has already been discussed a hundred times, I myself have read many questions on this issue, even this one , but this did not help me. In general, the problem is:

I get JSON and send them to the function:

let parsedData = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as! Dictionary<String, AnyObject> let name = parsedData["name"] as! String let latit = Double(parsedData["latit"] as! String) let longit = Double(parsedData["longit"] as! String) self.addPin(name: name, latit: latit!, longit: longit!) 

I get them in this function:

 func addPin(name: String, latit: Double, longit: Double) { let coord = CLLocationCoordinate2DMake(latit,longit) let pin = MGLPointAnnotation() pin.coordinate = coord pin.title = name mapView.addAnnotation(pin) } 

And when I start, I get an error on this line:

 mapView.addAnnotation(pin) 

I can’t imagine where nil can come from. The data that the function gets checked is not nil. The variables coord and pin are also initialized normally. I call the JSON download function from another ViewController, can it be something like this related to it?

If this is important, I use not the standard MapView, but MapBox

UPD:

After the commentary, I first thought that the problem might be due to the fact that I did not specify self.mapView, but by running the following code:

 func addPin(name: String, latit: Double, longit: Double) { let coord = CLLocationCoordinate2DMake(latit,longit) let pin = MGLPointAnnotation() pin.coordinate = coord pin.title = name print(self.mapView ?? "Пустой") self.mapView.addAnnotation(pin) } 

I get the message "Empty". Can mapView become empty due to the fact that I initialized the class with mapView in another controller to use some functions like this:

 let h = Hello() 

This is an action in which I initialize the control from which I'm trying to call the function:

 @IBAction func typeButtonTouchedUpInside(_ sender: UIButton) { print(sender.currentTitle ?? "Nil") let h = Hello() if(sender.currentTitle=="1") { h.getOrganizationFromRubricID(id: 0) } else if(sender.currentTitle=="2") { h.getOrganizationFromRubricID(id: 1) } else if(sender.currentTitle=="3") { h.getOrganizationFromRubricID(id: 2) } } 

The function that I call:

 public func getOrganizationFromRubricID(id: Int) { let urlString = "http://192.168.1.10/api/?getOrganizationsFromRubricID/id=\(id)" let url = URL(string:urlString) URLSession.shared.dataTask(with:url!) { (data, response, error) in if error != nil { print(error ?? "Something wrong") } else { do { self.removePins() let parsedData = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as! [Dictionary<String, AnyObject>] var i = 0 while i<parsedData.count { let name = parsedData[i]["name"] as! String let latit = Double(parsedData[i]["latit"] as! String) let longit = Double(parsedData[i]["longit"] as! String) self.addPin(name: name, latit: latit!, longit: longit!) i += 1 } } catch let error as NSError { print(error) } } }.resume() } 

And mapView itself through Storyboard created, not by code.

  • On the line above, print (mapView) see if your mapView is empty - Max Mikheyenko
  • @MaxMikheyenko thanks for the answer, proapdatel question - Razorik
  • Well, why unexpectedly found nil figured out. Now you need to look at the whole lifecycle of your mapView in order to understand where it disappears - Max Mikheyenko
  • @MaxMikheyenko mapView disappears when I, in another ViewController, initialize the ViewController where this mapView is located. How then can I call a function from Control1 that is in Control2? - Razorik
  • that is, you create a mapView in controller1 and then transfer it to controller2? - Max Mikheyenko

0