2

I'm just integrating Siesta and I love it, it solves a lot of issues we have when using frameworks like RestKit.

What I can't get my head around is how to use the content transformers? I've looked at the docs and examples and I can't quite understand how it works, I'm also fairly new to Swift.

Looking at this example taken from another SO reply:

private let SwiftyJSONTransformer = ResponseContentTransformer(skipWhenEntityMatchesOutputType: false) {
    JSON($0.content as AnyObject)
}

I can't quite understand what's going on here, there is no return value so I don't understand how content is being transformed. This might be my due to a lack of deep Swift knowledge.

I've understand how NSValueTransformer objects work in Obj-C but I can't work out how to map a response abit JSON or just a simple response body like a single string, number of boolean value to a object or type using Siesta.

We have some API responses that return just a single BOOL value in the response body while most of the other API responses are complex JSON object graphs.

How would I go about mapping these responses to more primitive types and or more complex objects.

Thanks.

Camsoft
  • 11,718
  • 19
  • 83
  • 120

1 Answers1

3

Some of your confusion is basic Swift stuff. Where a closure uses $0 and contains only a single statement, the input types are inferred and the return is implicit. Thus the code in your question is equivalent to:

ResponseContentTransformer(skipWhenEntityMatchesOutputType: false) {
  (content: AnyObject, entity: Entity) in
  return JSON(content)
}

(Using $0.content instead of just $0 is a workaround for a maybe-bug-maybe-feature in Swift where $0 becomes a tuple of all arguments instead of just the first one. Don’t worry too much about it; $0.content is just a magic incantation you can use in your Siesta transformers.)

The other half of your confusion is Siesta itself. The general approach is as follows:

  1. Configure a generic transformer that turns the raw NSData into a decoded but unstructured type such as String or Dictionary.
  2. Optionally configure a second transformer that turns the unstructured type into a model.
    • You’ll usually configure this based on API path.
    • Siesta doesn’t include any of this by default; it’s all per app.

For responses that are just a bare boolean, you’d probably do only #1 — depending on exactly what kind of response the server is sending, and depending on how you know it's just a boolean.

I recommend looking at the example project included with Siesta, which gives a good example of how all this plays out. You’ll see examples of both transformers that conditionally operate on the content based on its type (#1) and model-specific tranformers (#2) in that code.

Paul Cantrell
  • 9,175
  • 2
  • 40
  • 48
  • Thanks for this really helpful. It's a shame Swift can become this hard to understand by hiding types and arguments. Feels like it's gone too far in regards to being less verbose, to the point that it's hard to see what's going on. – Camsoft Mar 21 '16 at 10:03
  • Stick with it. It gets quite pleasant as you get familiar with it. – Paul Cantrell Mar 22 '16 at 16:34
  • Will try ;-) Thanks. – Camsoft Mar 23 '16 at 10:25