<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: &amp;quot;map&amp;quot; Fatal Error : unexpectedly found nil while implicitly unwrapping an Optional value in ArcGIS Runtime SDK for iOS Questions</title>
    <link>https://community.esri.com/t5/arcgis-runtime-sdk-for-ios-questions/quot-map-quot-fatal-error-unexpectedly-found-nil/m-p/1312536#M7639</link>
    <description>&lt;P&gt;Beware the exclamation mark. This code is dangerous:&lt;/P&gt;&lt;LI-CODE lang="swift"&gt;var map: AGSMap!&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Declaring &lt;STRONG&gt;map&lt;/STRONG&gt; as an &lt;A href="https://www.hackingwithswift.com/example-code/language/what-are-implicitly-unwrapped-optionals" target="_self"&gt;implicitly unwrapped optional&lt;/A&gt; is stating that even though the variable is optional and can be &lt;STRONG&gt;nil&lt;/STRONG&gt;, that you KNOW that whenever you access it, it will not be &lt;STRONG&gt;nil&lt;/STRONG&gt;. Unfortunately, in your app, that's not the case.&lt;/P&gt;&lt;P&gt;When you click "Maps" a second time and &lt;STRONG&gt;SetMapExtent()&lt;/STRONG&gt; is called, the &lt;STRONG&gt;map&lt;/STRONG&gt; variable above is &lt;STRONG&gt;nil&lt;/STRONG&gt;. Make sure that it isn't and your code will get a bit further. But I would recommend not declaring it as an implicitly unwrapped optional in the first place.&lt;/P&gt;&lt;LI-CODE lang="swift"&gt;func SetMapExtent () {
    let envelope = AGSEnvelope (xMin:216985.909, yMin:375531.776, xMax:241940.565, yMax:415497.915, spatialReference: self.mapView.spatialReference)
    self.map.initialViewpoint = AGSViewpoint(targetExtent: envelope)
    self.mapView.map = map
}&lt;/LI-CODE&gt;&lt;P&gt;However, &lt;STRONG&gt;self.mapView.spatialReference&lt;/STRONG&gt; is also &lt;STRONG&gt;nil&lt;/STRONG&gt;, so the &lt;STRONG&gt;envelope&lt;/STRONG&gt; you create will have a &lt;STRONG&gt;nil&lt;/STRONG&gt; spatial reference. That will also probably cause problems.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 27 Jul 2023 14:02:34 GMT</pubDate>
    <dc:creator>Nicholas-Furness</dc:creator>
    <dc:date>2023-07-27T14:02:34Z</dc:date>
    <item>
      <title>"map" Fatal Error : unexpectedly found nil while implicitly unwrapping an Optional value</title>
      <link>https://community.esri.com/t5/arcgis-runtime-sdk-for-ios-questions/quot-map-quot-fatal-error-unexpectedly-found-nil/m-p/1312433#M7638</link>
      <description>&lt;P&gt;I am developing a Map Application having various GIS functionalities on different ViewControllers connected via Navigation Controller. Each ViewController having AGSMapView loading some layers on map. While opening and closing (going back to main menu) first map view controller, it is working fine. Switching over to the next ViewController from main menu, it is also working fine. But after closing the second view controller and opening again the first view controller, it gives fatal error as mentioned in subject. I am enclosing the complete app code as Zip file, screenshot showing error and GIF file of Simulator Screen Recording.&lt;/P&gt;&lt;P&gt;It would be very much appreciated if anybody can help me to resolve this issue.&lt;/P&gt;&lt;P&gt;Thanks in advance&lt;/P&gt;&lt;P&gt;Afroz Alam&lt;/P&gt;</description>
      <pubDate>Thu, 27 Jul 2023 07:18:32 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-runtime-sdk-for-ios-questions/quot-map-quot-fatal-error-unexpectedly-found-nil/m-p/1312433#M7638</guid>
      <dc:creator>AfrozAlam</dc:creator>
      <dc:date>2023-07-27T07:18:32Z</dc:date>
    </item>
    <item>
      <title>Re: "map" Fatal Error : unexpectedly found nil while implicitly unwrapping an Optional value</title>
      <link>https://community.esri.com/t5/arcgis-runtime-sdk-for-ios-questions/quot-map-quot-fatal-error-unexpectedly-found-nil/m-p/1312536#M7639</link>
      <description>&lt;P&gt;Beware the exclamation mark. This code is dangerous:&lt;/P&gt;&lt;LI-CODE lang="swift"&gt;var map: AGSMap!&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Declaring &lt;STRONG&gt;map&lt;/STRONG&gt; as an &lt;A href="https://www.hackingwithswift.com/example-code/language/what-are-implicitly-unwrapped-optionals" target="_self"&gt;implicitly unwrapped optional&lt;/A&gt; is stating that even though the variable is optional and can be &lt;STRONG&gt;nil&lt;/STRONG&gt;, that you KNOW that whenever you access it, it will not be &lt;STRONG&gt;nil&lt;/STRONG&gt;. Unfortunately, in your app, that's not the case.&lt;/P&gt;&lt;P&gt;When you click "Maps" a second time and &lt;STRONG&gt;SetMapExtent()&lt;/STRONG&gt; is called, the &lt;STRONG&gt;map&lt;/STRONG&gt; variable above is &lt;STRONG&gt;nil&lt;/STRONG&gt;. Make sure that it isn't and your code will get a bit further. But I would recommend not declaring it as an implicitly unwrapped optional in the first place.&lt;/P&gt;&lt;LI-CODE lang="swift"&gt;func SetMapExtent () {
    let envelope = AGSEnvelope (xMin:216985.909, yMin:375531.776, xMax:241940.565, yMax:415497.915, spatialReference: self.mapView.spatialReference)
    self.map.initialViewpoint = AGSViewpoint(targetExtent: envelope)
    self.mapView.map = map
}&lt;/LI-CODE&gt;&lt;P&gt;However, &lt;STRONG&gt;self.mapView.spatialReference&lt;/STRONG&gt; is also &lt;STRONG&gt;nil&lt;/STRONG&gt;, so the &lt;STRONG&gt;envelope&lt;/STRONG&gt; you create will have a &lt;STRONG&gt;nil&lt;/STRONG&gt; spatial reference. That will also probably cause problems.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 27 Jul 2023 14:02:34 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-runtime-sdk-for-ios-questions/quot-map-quot-fatal-error-unexpectedly-found-nil/m-p/1312536#M7639</guid>
      <dc:creator>Nicholas-Furness</dc:creator>
      <dc:date>2023-07-27T14:02:34Z</dc:date>
    </item>
    <item>
      <title>Re: "map" Fatal Error : unexpectedly found nil while implicitly unwrapping an Optional value</title>
      <link>https://community.esri.com/t5/arcgis-runtime-sdk-for-ios-questions/quot-map-quot-fatal-error-unexpectedly-found-nil/m-p/1313429#M7644</link>
      <description>&lt;P&gt;Dear Nicholas,&lt;/P&gt;&lt;P&gt;Thank you very much for your reply. I have tried by declaring with question mark i.e. &lt;STRONG&gt;var map : AGSMap?, &lt;/STRONG&gt;even it is not working. If it is not declared with implicitly unwrapped optional the how can add some operational layers later like in the code below;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;STRONG&gt;func&lt;/STRONG&gt;&lt;/SPAN&gt; Load_Municipality_Boundary&lt;SPAN&gt;() {&lt;/SPAN&gt;&lt;SPAN class=""&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN class=""&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;&lt;SPAN&gt;&lt;STRONG&gt;self&lt;/STRONG&gt;&lt;/SPAN&gt;.&lt;SPAN&gt;munFeatureTable&lt;/SPAN&gt; = AGSServiceFeatureTable(url: &lt;SPAN&gt;munCensus&lt;/SPAN&gt;)&lt;SPAN class=""&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN class=""&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;&lt;STRONG&gt;let&lt;/STRONG&gt;&lt;/SPAN&gt; munFeatureLayer = AGSFeatureLayer(featureTable: &lt;SPAN&gt;&lt;STRONG&gt;self&lt;/STRONG&gt;&lt;/SPAN&gt;.&lt;SPAN&gt;munFeatureTable&lt;/SPAN&gt;)&lt;/P&gt;&lt;P&gt;&lt;SPAN class=""&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;&lt;SPAN&gt;map&lt;/SPAN&gt;.operationalLayers.&lt;SPAN&gt;add&lt;/SPAN&gt;(munFeatureLayer)&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;SPAN class=""&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN&gt;&lt;STRONG&gt;let&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;SPAN&gt; censusFeatureTable = AGSServiceFeatureTable(url: &lt;/SPAN&gt;&lt;SPAN&gt;URL&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;string&lt;/SPAN&gt;&lt;SPAN&gt;: &lt;/SPAN&gt;"&lt;A href="https://services.gisqatar.org.qa/server/rest/services/Vector/Census_Municipality/FeatureServer/1" target="_blank" rel="noopener"&gt;https://services.gisqatar.org.qa/server/rest/services/Vector/Census_Municipality/FeatureServer/1&lt;/A&gt;"&lt;SPAN&gt;)!)&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN class=""&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;&lt;SPAN&gt;map&lt;/SPAN&gt;.&lt;SPAN&gt;tables&lt;/SPAN&gt;.&lt;SPAN&gt;addObjects&lt;/SPAN&gt;(&lt;SPAN&gt;from&lt;/SPAN&gt;: [censusFeatureTable])&lt;/P&gt;&lt;P&gt;&lt;SPAN class=""&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;&lt;SPAN&gt;mapView&lt;/SPAN&gt;.selectionProperties.&lt;SPAN&gt;color&lt;/SPAN&gt; = .&lt;SPAN&gt;yellow&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN class=""&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;&lt;SPAN&gt;&lt;STRONG&gt;self&lt;/STRONG&gt;&lt;/SPAN&gt;.&lt;SPAN&gt;munFeatureLayer&lt;/SPAN&gt; = munFeatureLayer&lt;/P&gt;&lt;P&gt;&lt;SPAN class=""&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/SPAN&gt;&lt;SPAN class=""&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN class=""&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;}&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Would you please suggest me the best way to declare map so that it never happens and app does not crash.&lt;/P&gt;&lt;P&gt;Warm regards,&lt;/P&gt;&lt;P&gt;Afroz&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 30 Jul 2023 07:51:26 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-runtime-sdk-for-ios-questions/quot-map-quot-fatal-error-unexpectedly-found-nil/m-p/1313429#M7644</guid>
      <dc:creator>AfrozAlam</dc:creator>
      <dc:date>2023-07-30T07:51:26Z</dc:date>
    </item>
    <item>
      <title>Re: "map" Fatal Error : unexpectedly found nil while implicitly unwrapping an Optional value</title>
      <link>https://community.esri.com/t5/arcgis-runtime-sdk-for-ios-questions/quot-map-quot-fatal-error-unexpectedly-found-nil/m-p/1314091#M7645</link>
      <description>&lt;P&gt;Declaring map as optional means that it can be nil at some point. Your crash was because by using "!" you had said "I know it will never be nil when I use it", but it turned out it was nil.&lt;/P&gt;&lt;P&gt;If you declare it as optional with "?", then Swift will check if the map is nil. You won't get a crash, but your code might be skipped. And you must include logic to acknowledge that it could be nil when you're using it. One common approach is optional binding using an "if let" block.&lt;/P&gt;&lt;P&gt;So your code above could be included in an "if let" block:&lt;/P&gt;&lt;LI-CODE lang="swift"&gt;func Load_Municipality_Boundary() {        
    if let map = map {
        self.munFeatureTable = AGSServiceFeatureTable(url: munCensus)        

        let munFeatureLayer = AGSFeatureLayer(featureTable: self.munFeatureTable)

        map.operationalLayers.add(munFeatureLayer)

        let censusFeatureTable = AGSServiceFeatureTable(url: URL(string: "https://services.gisqatar.org.qa/server/rest/services/Vector/Census_Municipality/FeatureServer/1")!)

        map.tables.addObjects(from: [censusFeatureTable])

        mapView.selectionProperties.color = .yellow

        self.munFeatureLayer = munFeatureLayer
    } else {
        print("Map is not set!!")
    }
}&lt;/LI-CODE&gt;&lt;P&gt;That "if {}" block will only execute if map is not nil. It's still up to you to make sure the "map" variable is set to an AGSMap instance, or you will see a "Map is not set!!" message written to the console.&lt;/P&gt;&lt;P&gt;Optional variables are a fundamental Swift pattern, and not something specific to the ArcGIS Runtime SDK for iOS. I suggest you familiarize yourself with how optional variables are used in Swift. There are many tutorials and articles available online, such as &lt;A href="https://www.freecodecamp.org/news/optional-types-in-swift/" target="_self"&gt;this one&lt;/A&gt;.&lt;/P&gt;&lt;P&gt;Ultimately though, you need to work out why your app is setting "map" to nil at some point, and make sure that you set the "map" variable to a new AGSMap again before you use it in the above code.&lt;/P&gt;</description>
      <pubDate>Tue, 01 Aug 2023 15:07:03 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-runtime-sdk-for-ios-questions/quot-map-quot-fatal-error-unexpectedly-found-nil/m-p/1314091#M7645</guid>
      <dc:creator>Nicholas-Furness</dc:creator>
      <dc:date>2023-08-01T15:07:03Z</dc:date>
    </item>
  </channel>
</rss>

