title: AnyObject to array in swift
body: <p>Following function is given:</p>
  
  <pre><code>class func collection(#response: NSHTTPURLResponse,
                         representation: AnyObject) -&gt; [City] {
      return []
  }
  </code></pre>
  
  <p>So this function should return an array of city objects. I have to somehow transform the <code>representation</code> variable that is of type AnyObject to a city array. </p>
  
  <p>I don't know what the exact type of representation is but I can do things like</p>
  
  <pre><code>println(representation[0])
  </code></pre>
  
  <p>and it will print the object. Any ideas how to transform representation to [City] array?</p>
  
  <h2>Update</h2>
  
  <p>Doing</p>
  
  <pre><code>println(representation as [City]) 
  </code></pre>
  
  <p>prints nil.</p>
  
  <p>City.swift:</p>
  
  <pre><code>final class City : ResponseCollectionSerializable {
  
      let id: String
      let name: String
  
      class func collection(#response: NSHTTPURLResponse, representation: AnyObject) -&gt; [City] {
          return []
      }
  }
  </code></pre>
  
  <p>This is just copy and pasted from <a href="https://github.com/Alamofire/Alamofire#generic-response-object-serialization" rel="nofollow">https://github.com/Alamofire/Alamofire#generic-response-object-serialization</a> It should serialize a JSON response into objects:</p>
  
  <pre><code>@objc public protocol ResponseCollectionSerializable {
      class func collection(#response: NSHTTPURLResponse, representation: AnyObject) -&gt; [Self]
  }
  
  extension Alamofire.Request {
  
      public func responseCollection&lt;T: ResponseCollectionSerializable&gt;(completionHandler: (NSURLRequest, NSHTTPURLResponse?, [T]?, NSError?) -&gt; Void) -&gt; Self {
          let serializer: Serializer = { (request, response, data) in
              let JSONSerializer = Request.JSONResponseSerializer(options: .AllowFragments)
              let (JSON: AnyObject?, serializationError) = JSONSerializer(request, response, data)
              if response != nil &amp;&amp; JSON != nil {
                  return (T.collection(response: response!, representation: JSON!), nil)
              } else {
                  return (nil, serializationError)
              }
          }
  
          return response(serializer: serializer, completionHandler: { (request, response, object, error) in
              completionHandler(request, response, object as? [T], error)
          })
      }
  }
  </code></pre>