evermeer / AlamofireJsonToObjects

An Alamofire extension which converts JSON response data into swift objects using EVReflection
Other
161 stars 28 forks source link

Is it able to obtain an array of object? #12

Closed red010182 closed 8 years ago

red010182 commented 8 years ago

For example, to get users array, or a user object. (assume user is a model and with proper function defined)

Is this library able to obtain an array of object as well as a single object?

evermeer commented 8 years ago

The base functionality of alamofireJsonToObjects is handled by the EVReflection library. For more information see https://github.com/evermeer/EVReflection It should be able to handle all data structures from and to json. Including arrays of objects or objects with properties that have an array of objects. Here you can find the AlamofireJsonToObjects test for an array of objects: https://github.com/evermeer/AlamofireJsonToObjects/blob/6e44974ffb151910bbfdeb1f41bbac9374a0727a/AlamofireJsonToObjectsTests/AlamofireJsonToObjectsTests.swift#L100-100

If you want to see a more complete demo of AlamofireJsonToObjects, then have a look at https://github.com/evermeer/EVWordPressAPI

rafcabezas commented 8 years ago

I'm confused as to how to deserialize a JSON array.

       Alamofire.request(.GET, req)
            .responseObject { (response: Result<Array<MyObjectType>, NSError>) in
        }

I get `Cannot invoke 'reponseObject' with an argument list of type ...'

Thanks for your help!,

Raf.

rafcabezas commented 8 years ago

I think I got it. Need to use .responseArray instead of .responseObject, right? I'm gonna give that a shot.

Update: Can't test yet -- my backend is broken, waiting on a co-worker to fix it :) But looks like .reponseArray should work.

evermeer commented 8 years ago

Yes, .responseArray should do the trick.