0

Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value.

That is the error that i get, i get the error at: mapView.addAnnotation(annotations). I really don't know what to do about it. i work in swift with a tablet menu and this is the firstviewcontroller. Please someone help me out. here is the code i have written:

import UIKit
import MapKit

class FirstViewController: UIViewController{
    
    @IBOutlet weak var mapView: MKMapView!
    fileprivate let locationManager:CLLocationManager = CLLocationManager()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // call annotations functions and put in annotationLocations
        createAnnotations(locations: annotationLocations)
        
        locationManager.requestWhenInUseAuthorization()
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.distanceFilter = kCLDistanceFilterNone
        locationManager.startUpdatingLocation()
        
        mapView.showsUserLocation = true
        
    }
    
    // annotations location(s)
    let annotationLocations = [
        ["title": "Esso Kampen, Europa-Allee 4, 8265 VB, Kampen ", "latitude": 52.5429, "longitude": 5.9193],
        ["title": "Esso Kampen, Installatieweg 7-a, 8263 BS, Kampen", "latitude": 52.56830596923828, "longitude":5.879432678222656],
        ["title": "Esso IJsselmuiden, Spoorstraat 10, 8271 RH, IJsselmuiden", "latitude": 52.55617141723633, "longitude":5.931605815887451],
        ["title": "Esso IJsselmuiden, Ondernemersstraat 12, 8271 RS, IJsselmuiden", "latitude":52.554344177246094, "longitude": 5.939723014831543],
        ["title": "Esso Westwal, Westwal 1, 8321 WG, Urk", "latitude":52.6515101, "longitude":5.6190208],
        ["title": "Esso Elburg, Havenkade 2, 8081 GP, Elburg", "latitude":52.450706, "longitude":5.8280084],
        ["title": "Esso Aalst, Maas-waalweg 1, 5308 NS, Aalst", "latitude":51.779178619384766, "longitude":5.141426086425781],
        ["title": "Firezone Aalten, De Driehoek 1, 7122 TL, Aalten", "latitude":51.9280956, "longitude":6.5643974],
        ["title": "Firezone Oldeboarn, Sudkant 15, 8495 KM, Aldeboarn", "latitude":53.046293, "longitude":5.888884],
        ["title": "Esso Alkmaar MLK, Martin Luther Kingweg 51, 1817 MK, Alkmaar", "latitude":52.6446464, "longitude":4.7293343],
        ["title": "Firezone Alkmaar Huiswaarderweg, Huiswaarderweg 2, 1817 JS, Alkmaar", "latitude":52.6458756, "longitude":4.7382983],
        ["title": "Fuwell Alkmaar, Smaragdweg 1, 1812 RJ, Alkmaar", "latitude":52.609012, "longitude":4.756206],
        ["title": "Texaco Alkmaar, Robbenkoog 10, 1822 BB, Alkmaar", "latitude":52.6420575, "longitude":4.7821996],
        ["title": "Esso Almelo HR Holstlaan, H.R. Holstlaan 91, 7609 RC , Almelo", "latitude":52.3281816, "longitude":6.6495139],
        ["title": "Firezone Almelo, Burg. Raveslootsingel 6, 7607 GK, Almelo", "latitude":52.34979, "longitude":6.66009],
        ["title": "Esso Almere, Koningsbeltweg 1, 1329 AA, Almere", "latitude":52.3751158, "longitude":5.2747946],
        ["title": "Esso Almere Markerkant, Markerkant 10/01 , 1316 AA, Almere", "latitude":52.385953, "longitude":5.217036],
        ["title": "Esso Almere Hogering, Audioweg 2, 1322 AV , Almere", "latitude":52.34921, "longitude":5.17451],
        ["title": "Esso Alphen a/d Rijn, Willem de Zwijgerlaan 37, 2406 HD, Alphen a/d Rijn", "latitude":52.19819, "longitude":4.41177],
        ["title": "Esso Amersfoort Hogeweg, Hogeweg 45, 3816 BM, Amersfoort", "latitude":50.87979, "longitude":5.93174],
        ["title": "Esso Amersfoort Neerduist, Rijksweg A1 noordzijde, 3821 CH, Amersfoort", "latitude":52.205585, "longitude":5.3844],
        ["title": "Esso Amstelveen, Ouderkerkerlaan 150, 1185 AG, Amstelveen", "latitude":52.297044, "longitude":4.865962],
        ["title": "Esso Amstelveen Beneluxbaan Oost, Beneluxbaan 10 Oost, 1183 ZZ, Amstelveen", "latitude":52.317709, "longitude":4.870159],
        ["title": "Esso Express Amstelveen Beneluxbaan West, Beneluxbaan 9 West, 1181 ZZ, Amstelveen", "latitude":52.317307, "longitude":4.869668],
        ["title": "Esso Vissers Amstenrade, Hommerterweg 292-2, 6436 AM, Amstenrade", "latitude":50.934569, "longitude":5.916655],
        ["title": "Esso Amsterdam J v Hasseltweg, Johan van Hasseltweg 73, 1021 KN , Amsterdam", "latitude":52.387029, "longitude":4.924821],
        ["title": "Esso Amsterdam Marnixstraat, Marnixstraat 250 , 1016 TL, Amsterdam", "latitude":52.369842, "longitude":4.877018],
        ["title": "Esso Amsterdam Meer en Vaart, Meer en Vaart 201, 1068 LA, Amsterdam", "latitude":52.35523, "longitude":4.81045],
        ["title": "Esso Amsterdam Sarphatistraat, Sarphatistraat 225, 1018 AE, Amsterdam", "latitude":52.36685, "longitude":4.92415],
        ["title": "Esso Amsterdam Pieter Caland, Pieter Calandlaan 89, 1065 KK, Amsterdam", "latitude":52.3559, "longitude":4.82452],
        ["title": "Esso Amsterdam Utrechtse Brug, Nieuwe Utrechtseweg 10, 1096 CK, Amsterdam", "latitude":52.33214, "longitude":4.91194],
        ["title": "Texaco Amsterdam Kollenbergweg, Kollenbergweg 11, 1101 AR, Amsterdam Zuidoost", "latitude":52.305681, "longitude":4.934042],
        ["title": "Esso Apeldoorn, Rijksweg A50 westzijde, 7372 BJ, Apeldoorn", "latitude":52.169628, "longitude":6.024606],
        ["title": "Firezone Apeldoorn Edisonlaan, Edisonlaan 270, 7316 JL, Apeldoorn", "latitude":52.2305509, "longitude":5.9604756],
        ["title": "Firezone Apeldoorn Laan van Zeven, Laan van Zevenhuizen 396, 7322 NW, Apeldoorn", "latitude":52.2207337, "longitude":5.9985234],
        ["title": "Esso Appingedam, Farmsumerweg 60, 9902 BT, Appingedam", "latitude":53.319883, "longitude":6.870897],
        ["title": "Esso Arnhem IJsseloordweg, IJsseloordweg 42, 6825 LE, Arnhem", "latitude":51.978645, "longitude":5.971316],
        ["title": "Esso Arnhem Overmaat, Burg. Matsersingel 30, 6831 ZA, Arnhem", "latitude":51.95184, "longitude":5.88583],
        ["title": "Esso Arnhem de Slenk, Rijksweg A50 oostzijde, 6816 VX, Arnhem", "latitude":52.1664735, "longitude":6.025448],
        ["title": "Esso Express Arnhem, Dr C Lelyweg 13, 6827 BH, Arnhem", "latitude":51.97244, "longitude":5.9348],
        ["title": "Texaco Asten, Rijksweg A67 zuidzijde, 5721 SG, Asten", "latitude":51.404306, "longitude":5.812127],
        ["title": "Esso Baarn Oost, Amsterdamsestraatweg 42a Oostzijde, 3741 GS, Baarn", "latitude":52.221659, "longitude":5.257677],
        ["title": "Esso Express Baarn West, Amsterdamsestraatweg 61 Westzijde, 3741 GS, Baarn", "latitude":52.221114, "longitude":5.257666],
        ["title": "Texaco Bakel, Benthem 3, 5761 PN, Bakel", "latitude":51.49906, "longitude":5.72809],
        ["title": "Firezone Beegden, Heerstraat Noord 60, 6099 AZ, Beegden", "latitude":50.97359, "longitude":5.76655],
        ["title": "Esso Vissers Beek en Donk, Heuvelplein 29, 5741 JH, Beek en Donk", "latitude":51.52807, "longitude":5.63457],
        ["title": "Firezone Beek, Arnhemseweg 13, 7037 CX, Beek Gem Bergh", "latitude":51.908966, "longitude":6.187974],
        ["title": "Texaco Beekbergen, Dorpsstraat 43, 7361 AR, Beekbergen", "latitude":52.1592, "longitude":5.96691],
        ["title": "Texaco Beers, Molenstraat 31-33, 5437 AA, Beers", "latitude":51.726731, "longitude":5.836205],
        ["title": "Esso Beesd, Rijksweg A2 westzijde, 4153 ZA, Beesd", "latitude":52.143648, "longitude":5.000866],
        ["title": "Firezone Belfeld, Craenenakker 4, 5951 CC, Belfeld", "latitude":51.31877, "longitude":6.12664],
        ["title": "Texaco Bellingwolde, Hoofdweg 33, 9695 AA, Bellingwolde", "latitude":53.1014, "longitude":7.14563],
        ["title": "Esso Beneden Leeuwen 1, Van Heemstraweg 35, 6658 KE, Beneden Leeuwen", "latitude":51.88036, "longitude":5.53006]
    ]
    
    // create annotation
    // [Dictionary<String, Any>] or [[String: Any]] works for argument type
    func createAnnotations(locations: [Dictionary<String, Any>]) {
        for location in locations {
            let annotations = MKPointAnnotation()
            annotations.title = location["title"] as? String
            annotations.coordinate = CLLocationCoordinate2D(latitude: location["latitude"] as! CLLocationDegrees,
                                                            longitude: location["longitude"] as! CLLocationDegrees)
            
                mapView.addAnnotation(annotations)
        }
    }
}
Rob
  • 415,655
  • 72
  • 787
  • 1,044
Marc
  • 3
  • 1
  • 1
    Does this answer your question? [What does "Fatal error: Unexpectedly found nil while unwrapping an Optional value" mean?](https://stackoverflow.com/questions/32170456/what-does-fatal-error-unexpectedly-found-nil-while-unwrapping-an-optional-valu) – EDUsta Sep 17 '20 at 18:23
  • It looks to me as if the mapView isn't initialized by the time you're making the addAnnotation call. I also noticed you haven't set the delegate for the mapview which may be a factor – Justin Frazer Sep 17 '20 at 18:31
  • How did you instantiate `FirstViewController`? Manually or via storyboard? If your outlets are hooked up properly, manually instantiating view controller incorrectly is a common source of this sort of problem. But we can’t comment further without information about how you instantiated this view controller. – Rob Sep 17 '20 at 19:38

1 Answers1

0

Reconnect the '@IBOutlet weak var mapView: MKMapView!'. Delete that line, then drag to reconnect it from the view in the storyboard to your code.

mafarja
  • 181
  • 11