JSON Array for object swift 3

0

Somebody help me and guide me, I'm studying iOS development and I put a "Alamofire" library with cocoapods to consume JSON until all right, but my JSON returns like this:

{
  "results":[
     {
        "id":7,
        "descricao":"Acupuntura",
        "campoOrderBy":"descricao"
     }
]
}

Are there any libraries that can put these faces in entities, attributes, variable in a simple way, have some article, website that can see this? Any tips on where to start studying?

    
asked by anonymous 18.10.2016 / 16:33

1 answer

0

You can use OCMapper :

JSON Example:

{
   "firstName"   : "FirstName",
   "lastName"    : "LastName",
   "age"         : 26,
   "dateOfBirth" : "01/01/2013",
   "address"     : { 
                        "city" : "San Diego", 
                        "country" : "US"  
                   },
   "posts"       : [
                         {
                             "title" : "Post 1 title",
                             "datePosted : "04/15/2013",
                         },
                         {
                             "title" : "Post 2 title",
                             "datePosted : "04/12/2013",
                         }
                   ]
}

An object with the JSON data:

@objc public class User: NSObject {

    var firstName: String?
    var lastName: String?
    var age: NSNumber?
    var dateOfBirth: NSDate?
    var address: Address?
    var posts: [Post]? // Repare que como é um array no JSON, será necessário criar outro Objeto chamado Post para receber os dados de dentro do array
}

Swift Usage:

let user = ObjectMapper.sharedInstance().objectFromSource(dict, toInstanceOfClass:User.self) as User

Using Objective-C:

User *user = [[ObjectMapper sharedInstance] objectFromSource:dictionary toInstanceOfClass:User.class];

It is even possible to integrate Alamofire so that it is already the parser of the request, has an example in the link above.

    
18.10.2016 / 17:10