How to define content transformers in Siesta? - ios

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.

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:
Configure a generic transformer that turns the raw NSData into a decoded but unstructured type such as String or Dictionary.
You’ll usually configure this based on content type.
Siesta includes parsing for strings, JSON, and images unless you turn it off with useDefaultTransformers: false.
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.

Related

why Objective-C convert to swift wrong

why

-(void)addSimpleListener:(id<XXSimpleListener>)listener
convert to swift look like this:
func add(_ listener: XXSimpleListener?) {
}
but change the method to this

-(void)addSimpleListener:(id<XXSimpleListening>)listener

and it will convert to this
func addSimpleListener(_ listener: XXSimpleListening?){
}
Xcode (or whatever tool you are using to do the conversion) is merely following Swift API guidelines. Specifically:
Omit needless words. Every word in a name should convey salient information at the use site.
More words may be needed to clarify intent or disambiguate meaning, but those that are redundant with information the reader already possesses should be omitted. In particular, omit words that merely repeat type information.
In the first case, the words SimpleListener in addSimpleListener is repeating the type of the parameter, so they are removed from the method name. However, in the second case, SimpleListener and SimpleListening does not look the same to whatever tool you are using, so it thinks that SimpleListener should be kept.
In my (human) opinion though, I think the method should be named addListener, because:
Occasionally, repeating type information is necessary to avoid ambiguity, but in general it is better to use a word that describes a parameter’s role rather than its type.
Listener is the role of the parameter.

How to return a struct from an imported DLL-function in MQL4?

Is there a way to return a struct from an imported function in MQL4, without having to pass it as a parameter and making a memcpy?
Be cautious with any kind of DLL-interfacing, MQL4 Documentation states:
Passing ParametersAll parameters of simple types are passed by values unless it is explicitly indicated that they are passed by reference. When a string is passed, the address of the buffer of the copied string is passed; if a string is passed by reference, the address of the buffer of this string without copying it is passed to the function imported from DLL.Structures that contain dynamic arrays[], strings, classes, other complex structures, as well as static or dynamic arrays[] of the enumerated objects, can't be passed as a parameter to an imported function.When passing an array to DLL, the address of the beginning of the data buffer is always passed (irrespective of the AS_SERIES flag). A function inside a DLL knows nothing about the AS_SERIES flag, the passed array is a static array of an undefined length; an additional parameter should be used for specifying the array size.
More glitches apply... Then how to make it work?
Maybe a straight, heterogeneous multi-party distributed processing, which communicates rather results than function calls, independent of all nightmares of maintaining just DLL-imported functions API changes, is a way safer way to go. Using this approach for the last few years and since than have no problems with New-MQL4.56789 string-s that seized to remain string-s and silently started to become struct-s etc.
Worth to know about.
Anyway, welcome and enjoy the Wild Worlds of MQL4 -- may enjoy to click and read other posts on issues in MQL4/DLL integration and/or signalling/messaging in MQL4 domains. Feel free to ask more

Proper way to pull JSON API response and store or reference it in Swift?

I'm interested in knowing if there's a "proper" way to handle JSON responses from API requests.
Right now I have a struct that contains a static var. In the AppDelegate I grab the JSON from a URL (using Alamofire). I am able to reference it later in different views, however I have read that there are better ways than a "struct hack" to handle this.
My goal is to minimize the amount of calls, and only refresh when it's appropriate (ie. user "pulls down to refresh", "new object inserted into database", etc). Should this be how I handle it, or should I load different JSON responses on each new view, depending on what's required?
Any direction is appreciated.
Edit: Here's where I read that Structs were a "hack" for a "global variable". Maybe storing JSON in a struct is different than keeping a typical user variable in one. I don't know. See the second Answer's remarks.
Global Variables in Swift
As for the Struct I have:
In my jsonDataHolder.swift file:
struct jsonDataHolder {
static var jsonData:AnyObject = []
}
In my AppDelegate.swift file, I have the following in DidFinishLaunchingWithOptions section:
Alamofire.request(.GET, "http://xxxxxxxxxx.com/yyy", encoding: .JSON).responseJSON { (_, _, JSONData, _) in
jsonDataHolder.jsonData = JSONData!
}
Again, this all works fine. Just not sure if it's the "right" (if there is such a thing) way to do it.
If I understand the question correctly, though, this seems like the oft debated "singletons are evil" discussion. As we are supposed to avoid debating opinions here on Stack Overflow, I'll simply refer you to What is so bad about singletons?.
I must confess, though, that what I find more troubling is the the lack of any clear interface for this JSON structure. I'd rather see a model object with some logical public interface.
With this current object, your entire app (or at least everything that interfaces with this object) is dependent upon the private implementation details of the JSON.
The advice to avoid singleton-style patterns should not be accepted as dogma, but you should consider the reasons why that advice is generally offered (complicates testing, results in inappropriately tightly-coupled code, etc.). If you're adopting pattern like AnyObject that obscures implementation-dependent JSON structure, then there's no point in worrying about the singleton pattern, because you've already baked all of these limitations/problems in your code already.

Should I use NSCoder to serialise to JSON?

There are a number of questions and answers on SO that ask how do I serialise an object to in objective c.
Serialize and Deserialize Objective-C objects into JSON
Objective C serialize list of complex objects
Serialize custom object to JSON which contains NSMutableArray
How to serialize a class in IOS sdk (Objective-c)?
The following 3 methods are all mentioned in the above links.
1) Use NSJSONSerialization to serialise the object to JSON. Seems good but this requires the object in question to be either an array or dictionary at its top level. Common solution is to declare custom toDictionary or serialise method that loops over the properties and sets the relevant key and values.
2) Conform to the NSCoder protocol, a little like the approach above but there seems to be some confusion around whether or not this can serialise to JSON or just to NSData.
3) Third party library.
I'm getting slightly confused as to what approach to take. I want to serialise to JSON, there are contradictory answers some stating you can use NSCoder some saying not. I know that a third party app will work however I'd rather implement something simple like options 1 or 2.
Thoughts?
With 1, you'd basically be writing a JSON-based implementation of NSCoder from scratch. Certainly doable, though.
With 2, I believe it might be possible, since I think the output of NSCoder is some variant of XML (though compressed into a binary blob). However, I don't know if this is a great approach, since the format is proprietary and not really meant to be human-editable. There might also be a mismatch between what's allowed in JSON vs. the NSCoder format in terms of keys and leaf nodes, forcing you to do a messy conversion.
I've been trying to do something similar, and based on my research, I actually suggest 3. Using something like Mantle — a stable, polished framework that gets frequent updates — you can specify exactly how your model objects will be serialized to and deserialized from JSON. It even supports the NSCoder protocol as an option! (This is effectively solution 1, but vetted and maintained by a 3rd party.)

Serialize Flex Objects to Save/Restore Application State

Is it possible to serialize a hierarchy of objects in Flex, send the binary data to a URL for storage/retrieval on/from a server, and deserialize the data to restore the objects' original state?
I know it's possible to convert the objects into an XML format (haven't tried it yet), but I'm hoping to avoid parsing XML and rebuilding the objects manually. It would be nice to have functionality which can serialize/deserialize objects to a simple binary format (I did something similar in the past in Java, though not quite as easily as I would have liked). The 'eval' function in Perl is similar to what I'm looking for, sans saving code, of course.
In pseudo-code, here's what I would like to do:
private var contentToSave:HBox = new HBox();
private function saveState(event:Event):void {
var toSave:HBox = this.contentToSave;
var data:? = /* serialize 'toSave' ActionScript classes to binary data*/;
sendDataToServer(data, filename);
}
private function restoreState(filename):void {
var data:? = getDataFromServer(filename);
var savedData:HBox = /* deserialize binary 'data' to ActionScript classes */;
this.contentToSave = savedData;
}
Take a look at ByteArray.writeObject(). which saves the passed object in AMF format into the byte array. I have not used this function too much, I don't exactly know what kind of objects it can serialize, but definitely not all.
Try the JSON based serialization package in ascorelib.
[...]but I'm hoping to avoid parsing XML and rebuilding the objects manually
AS handles XML just like any other native type. Rest assured. XML is the preferred way of dealing with data you will be pulling off and putting back on a server. Of course, the ascorelib gives you a JSON class -- so you may want to look at that as well.
The 'eval' function in Perl is similar to what I'm looking for, sans saving code, of course.
IIRC, eval is part of the ECMAScript specification (and you will find it in Javascript). But not in AS3.0. It was there to a certain extent in some previous version(s?) but is no longer supported.

Resources