title: How can I cast an NSMutableArray to a Swift array of a specific type?
body: <p>I am migrating my iOS project to Swift. I am doing this class by class. When I call Objective C methods from Swift, a lot of Objective C types are converted to their Swift counterparts.</p>
  
  <p>In my case an Objective C <code>NSMutableArray</code> gets converted to Swift's <code>Array&lt;AnyObject&gt;</code>. Now here comes my problem. Within my Swift class, I get such an array back from an Objective C object. Now that I am in the Swift world, I would like to cast this array to a specific type instead of <code>AnyObject</code>, because I know for sure what kind of objects exist in this array.</p>
  
  <p>The compiler won't let me do that! Let me simplify my problem by saying I want to cast to an array containing strings. This is what I tried:</p>
  
  <pre><code>var strings = myObjcObject.getStrings() as [String]
  </code></pre>
  
  <p>I get the following error from the compiler:</p>
  
  <blockquote>
    <p>'String' is not identical to 'AnyObject'</p>
  </blockquote>
  
  <p>I would have to agree with the compiler, since String is indeed not identical to AnyObject. But I don't see why that is a problem. I can downcast AnyObject to String if I want, right?</p>
  
  <p>I also tried:</p>
  
  <pre><code>var strings = myObjcObject.getStrings() as? [String]
  </code></pre>
  
  <p>This seems to be a step in the right direction, but getStrings() returns an <code>NSMutableArray</code> so I get the following error:</p>
  
  <blockquote>
    <p>'NSArray' is not a subtype of 'NSMutableArray'</p>
  </blockquote>
  
  <p>Is there any way to do what I am trying to do here?</p>