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.