Mikel Sanchez Mikel Sanchez - 3 months ago 30
iOS Question

ObjectMapper fails with root element

I'm facing an issue that I don't know exactly how to solve.

When I try to map this JSON to User object it fails:

SwiftyJSON at "2.3.3"

JSON:

{
"results":[
{
"gender":"male",
"name":{
"title":"mr",
"first":"donald",
"last":"boor"
},
"location":{
"street":"1781 vredenburg",
"city":"het bildt",
"state":"noord-brabant",
"postcode":30431
},
"email":"donald.boor@example.com",
"login":{
"username":"greenpeacock269",
"password":"lambda",
"salt":"2uqekB9c",
"md5":"4fd97ba849fc6d1abb23134cd554dba8",
"sha1":"d5adf8eadc9c8b33422ccbc8087333e6dec6f83e",
"sha256":"2e4b3344d72dc6937fee422149146cb83b6397530e6afdb12daad165abe2b3d9"
},
"dob":"1986-12-25 21:32:21",
"registered":"2016-02-25 06:58:50",
"phone":"(365)-252-9092",
"cell":"(962)-625-3483",
"id":{
"name":"BSN",
"value":"59041461"
},
"picture":{
"large":"https://randomuser.me/api/portraits/men/15.jpg",
"medium":"https://randomuser.me/api/portraits/med/men/15.jpg",
"thumbnail":"https://randomuser.me/api/portraits/thumb/men/15.jpg"
},
"nat":"NL"
}
],
"info":{
"seed":"806e92dbf96026d0",
"results":1,
"page":1,
"version":"1.1"
}
}


I want to map to my user class:

import Foundation
import ObjectMapper

class User: NSObject, Mappable {
var gender: String?
var title: String?
var first: String?
var last: String?

required init?(_ map: Map) {

}

// Mappable
func mapping(map: Map) {
gender <- map["gender"]
title <- map["name.title"]
first <- map["name.first"]
last <- map["name.last"]
}
}

let user = Mapper<User>().mapArray(jsonData) //Doesn't work getting nil
let userList :Array<User> = Mapper<User>().mapArray(jsonData)! //fatal error: unexpectedly found nil while unwrapping an Optional value


RESPONSE CODE:

let dataTask = session.dataTaskWithRequest(request, completionHandler: { (data, response, error) -> Void in

if (error == nil) {
do {
//let jsonResult: NSDictionary = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as! NSDictionary
let jsonResult = JSON(data: data!)
let dict = jsonResult["results"].dictionaryValue
let userList :Array<User> = Mapper<User>().mapArray(dict)!
print(userList)


} catch {}
}

Answer

Since you have not provided the code where you are receiving the response, I have tested using test case with the response being put into test.json file.

if let path = NSBundle.mainBundle().pathForResource("test", ofType: "json") {
    do {
        let jsonData = try NSData(contentsOfFile: path, options: NSDataReadingOptions.DataReadingMappedIfSafe)
        do {
            let jsonResult: NSDictionary = try NSJSONSerialization.JSONObjectWithData(jsonData, options: NSJSONReadingOptions.MutableContainers) as! NSDictionary

            let userList :Array<TestUser> = Mapper<TestUser>().mapArray(jsonResult.objectForKey("results"))!
            print(userList)


        } catch {}
    } catch {}
}

EDIT:

I have implemented SwiftyJSON syntax, try the following line of code:

let userList :Array<TestUser> = Mapper<TestUser>().mapArray(jsonResult["results"].arrayObject)!
print(userList)

Change Model Class to:

class TestUser: NSObject, Mappable {
    var gender: String?
    var title: String?
    var first: String?
    var last: String?

    required init?(_ map: Map) {

    }

    // Mappable
    func mapping(map: Map) {
        gender      <- map["gender"]
        title       <- map["name.title"]
        first       <- map["name.first"]
        last        <- map["name.last"]
    }
}