user793789 user793789 - 5 months ago 31
Swift Question

Siesta Swift Casting from ImplicitlyUnwrappedOptional<Swift.AnyObject> to Array<AnyObject>

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).

Answer

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() ?? []