简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM