evermeer / AlamofireJsonToObjects

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

Mapping list of objects? #34

Closed Dbigshooter closed 8 years ago

Dbigshooter commented 8 years ago

Hi there,

I'm trying to map the following JSON:

[{"id":121, "active":true}]

Into the following object:

class UserPackageTransfer: EVObject, Mappable {

    var userPackages = [UserPackage]()
...

`class UserPackage : EVObject, Mappable {

var id : Int = 0
var active: Bool = false`

...

I try to map it this way:

Alamofire.request(apiURL + "?userid=2", method: .get, parameters: nil, encoding: URLEncoding.default, headers: headers()).responseObject { (response: DataResponse) in

But the object I get is empty, and I'm left with:

Optional(UserPackageTransfer {
   hash = 0
   userPackages = (
)
}

Any suggestions?

evermeer commented 8 years ago

The json result is an array of UserPackage object. This is what AlamofireJsonToObject can return from the call. So you can do something like:

        Alamofire.request(apiURL + "?userid=2")
        .responseArray { (response: DataResponse<[UserPackage]>) in
             let result = UserPackageTransfer()
             result.userPackages = response.result.value
       }
Dbigshooter commented 8 years ago

@evermeer Worked like a charm, thanks!