title: Value of optional type 'String?' must be unwrapped to a value of type 'String' to an array?
body: <p>Total new beginner here in Swift, I'm trying to understand typecasting here</p>
  <p>I have the below code</p>
  <pre class="lang-swift prettyprint-override"><code>    @IBAction func randomImage(_ sender: Any) {
          let path = Bundle.main.path(forResource: &quot;imageList&quot;, ofType: &quot;plist&quot;)
          let dict = NSDictionary(contentsOfFile: path!)
          
          let data = dict!.object(forKey: &quot;Images&quot;) as! [String]
          
          imageView.image = UIImage(named: data.randomElement())
          
      }
  </code></pre>
  <p>As shown above, first I have <code>dict!</code> to ensure that <code>dict</code> is available, then <code>data</code> will be typecasted into <code>[String]</code> which is an array of string.</p>
  <p>Now the part that I dont understand is why <code>data.randomElement()</code> giving me error</p>
  <blockquote>
  <p>Value of optional type 'String?' must be unwrapped to a value of type 'String'</p>
  </blockquote>
  <blockquote>
  <p>Coalesce using '??' to provide a default when the optional value contains 'nil'</p>
  </blockquote>
  <blockquote>
  <p>Force-unwrap using '!' to abort execution if the optional value contains 'nil'</p>
  </blockquote>
  <p>sure enough based on the suggestion, i can get away with <code>data.randomElement()!</code>, but why is that needed?</p>