3

I am configuring ResponseTransformer of siesta to return array of objects.

    service.configureTransformer("/models/*") {
        Model.instantiate($0.content)
    }

but somehow when I try to convert them to back to array using let objects = response.content as! [Object] I got this exception Could not cast value of type 'Swift.ImplicitlyUnwrappedOptional<Swift.AnyObject>' (0x382a0a0) to 'Swift.Array<Object>' (0x16f5358).

Paul Cantrell
  • 9,175
  • 2
  • 40
  • 48
user793789
  • 196
  • 1
  • 13
  • You could inspect the dynamic type of the value wrapped in the optional `response.content` by writing `if let content = response.content { print(content.dynamicType) }` – tonisuter Apr 09 '16 at 05:56

1 Answers1

1

You need to map your response, like this

configureTransformer("/models/*") {
    ($0.content).map(Model.instantiate)
}

And to get later, you can try this way

let objects = resource.typedContent() ?? []
haroldolivieri
  • 2,173
  • 18
  • 29
  • 1
    Note that you can also do `resource.typedContent(ifNone: [])`. This has the advantage that you can fine-tune the type inference using the `ifNone` param: `resource.typedContent(ifNone: [Model]())`. – Paul Cantrell Jun 12 '16 at 18:09