title: Can an [AnyObject] array be optionally downcast to a type-specific array?
body: <p>I'm reading through the Swift documentation, looking at the section regarding type casting.</p>
  
  <p>The documentation talks about getting an array of type <code>[AnyObject]</code> from Foundation frameworks stuff (what would be an <code>NSArray *</code> in Objective-C).</p>
  
  <p>First, the documentation provides this example:</p>
  
  <pre><code>for object in someObjects {
      let movie = object as Movie
      println("Movie: '\(movie.name)', dir. \(movie.director)")
  }
  </code></pre>
  
  <p>Now, I want to change the example slightly, to a case where I don't know all the objects are of type <code>Movie</code>, so I'd do this:</p>
  
  <pre><code>for object in someObject {
      if let movie = object as? Movie {
          println("Movie: '\(movie.name', dir. \(movie.director)")
      }
  }
  </code></pre>
  
  <p>The documentation then provides an example of a better way to write the first loop:</p>
  
  <pre><code>for movie in someObjects as [Movie] {
     println("Movie: '\(movie.name)', dir. \(movie.director)")
  }
  </code></pre>
  
  <p>Where we downcast <code>someObjects</code> from an <code>[AnyObject]</code> to a <code>[Movie]</code> so we don't have to downcast within the loop.</p>
  
  <p>And this got me thinking, can the array be option downcast as a whole?</p>
  
  <pre><code>if let someMovies = someObjects as? [Movie] {
      for movie in someMovies {
          println("Movie: '\(movie.name)', dir. \(movie.director)")
      }
  }
  </code></pre>
  
  <p>Does this work?  And if so, how bad is this from a performance standpoint?  How long would it take to check the type of every object in a 10,000 element array using the optional downcast?</p>
  
  <p>I understand that the implications between this snippet and my previous optional downcast snippet are different.  The first will iterate through every object and only attempt to print if the object is a <code>Movie</code>, where the second will only enter the loop if the array can be downcast to a <code>[Movie]</code> array, in which case it will either print all or none, but I can imagine there are situations where this would be preferable.</p>