Search code examples
iosswiftmkmapview

Mapview nil closure, saw topics on StackO. but didn't help me


I have read this topic which users let me check How to access ViewController component from PageViewController

But it didn't help me, and the mapView is still nil. I also read this, but didn't help http://www.apeth.com/swiftBook/ch04.html#SECinstanceRefs Could anyone please review/see my code and help me instead of linking to another topic which isn't helping at all?

I have this function in the class Driver

private static let _instance = Driver();

static var Instance: Driver {
    return _instance;
}

func getAllDriversLocations(){

    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let vc = storyboard.instantiateViewController(withIdentifier: "ViewController") as! ViewController;

    var location : [CLLocation] = []

    Account.Instance.dbRef.child("driverLocations").observeSingleEvent(of: .value, with: { (snapshot) in
        if let data = snapshot.value as? NSDictionary {
            if let loca = data as? [String: [String:Any]]{
                let keys = loca.keys
                let sortedKeys = Array(keys).sorted(by: { $0 > $1 }) // highest will be the first
                _ = sortedKeys[0]
                //let intHighPlus = Int(highestKey)! + 1
                for index in 0...sortedKeys.count-1{

                    let stringIndex = "0\(index+1)"
                    if let indexChecked = loca[stringIndex]{
                        if let doubleLatString = (indexChecked["latitude"]){
                            let latDouble = (doubleLatString as! NSString).doubleValue

                        if let doubleLongString = (indexChecked["longitude"]){
                            let longDouble = (doubleLongString as! NSString).doubleValue
                            location.append(CLLocation(latitude: (latDouble), longitude: (longDouble)))
                            }
                        }
                    }
                }
                if location.count > 0{
                    vc.placeAllCabPlacemarks(array: location)
                }
            }
        }
    })
}

So here I call placeAllCabPlacemarks, the location array is getting correctly in the placeAllCabPlacemarks function but then I get the problem that mapView is nil here:

@IBOutlet weak var myMap: MKMapView?

override func viewDidLoad() {
    super.viewDidLoad()

    myMap?.delegate = self
    initializeLocationManager()
    Driver.Instance.getAllDriversLocations()
}
func placeAllCabPlacemarks(array: [CLLocation]){
    //print("locs: \(array)")
    let arraySize = array
    for index in 0...arraySize-1 {
        let annotation = MKPointAnnotation()
        annotation.coordinate = CLLocationCoordinate2D(latitude: array[index].coordinate.latitude, longitude: array[index].coordinate.longitude)
            print("map \(myMap)") //Here it's nil
            myMap?.addAnnotation(annotation as MKAnnotation)
        }
}

I really checked everything, also a couple of items on internet & stackoverflow. Could anybody please check why this isn't working? The main problem is that the mapView is nil...


Solution

  • So the problem where the answers that I was linked to in the previous post...

    The first problem was that I used singletons at:

    Driver.Instance.getAllDriversLocations()
    

    I changed that to:

    let driver = Driver() //instance of the class driver
    driver.getAllDriversLocations(callBack: self)
    

    The second problem was this code:

    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let vc = storyboard.instantiateViewController(withIdentifier: "ViewController") as! ViewController;
    

    I changed that code to this:

    func getAllDriversLocations(callBack : ViewController){
    
        let vc = callBack
    }
    

    I hope I help someone with this one day, instead of making it worst like the previous post I've linked to did..