0

I am trying to figure out an error: fatal error: unexpectedly found nil while unwrapping an Optional value

class MainViewController: UIViewController {
    var mapView :AGSMapView?
    mapViewHelper = GeoneMapViewHepler.init(mapView: mapView!, mapViewDelegate: self)
}

class GeoneMapViewHepler: NSObject {

    var mapViewModule :GeoneMapViewModel?
    var mapView :AGSMapView?
    var mapViewDelegate :AnyObject?

init(mapView mp: AGSMapView, mapViewDelegate md: AnyObject) {
    mapView = mp
    mapViewDelegate = md
    super.init()
    }

}

I spent half a day,but I don't find the key...

thanks for answers !

vaibhav
  • 4,038
  • 1
  • 21
  • 51
Mrdten
  • 13
  • 4
  • this error is obvious because the optional mapView var is nil and you are assigning the value to the required variable inside the init function. – Mahesh Agrawal Aug 17 '16 at 06:55
  • you should have to have a look of this [answer](http://stackoverflow.com/questions/32170456/what-does-fatal-error-unexpectedly-found-nil-while-unwrapping-an-optional-valu) this may help ..and the problem is sure accessing nil object by you. – vaibhav Aug 17 '16 at 07:17
  • thanks,it has been solved.^_^ – Mrdten Aug 17 '16 at 08:18

1 Answers1

0

Put MainViewController's initialization code in viewDidLoad. mapView is set only once the view has loaded.

func viewDidLoad() {
    super.viewDidLoad()

    mapViewHelper = GeoneMapViewHepler.init(mapView: mapView!, mapViewDelegate: self)

    ...
}
Khanh Nguyen
  • 11,112
  • 10
  • 52
  • 65