1

Suppose that I have a User object:

class User: Mappable {
    var username: String?
    var age: Int?
    var weight: Double!
    var array: [Any]?
    var dictionary: [String : Any] = [:]
    var bestFriend: User?                       // Nested User object
    var friends: [User]?                        // Array of Users
    var birthday: Date?

    required init?(map: Map) {

    }

    // Mappable
    func mapping(map: Map) {
        username    <- map["username"]
        age         <- map["age"]
        weight      <- map["weight"]
        array       <- map["arr"]
        dictionary  <- map["dict"]
        bestFriend  <- map["best_friend"]
        friends     <- map["friends"]
        birthday    <- (map["birthday"], DateTransform())
    }
}

And my Json contains an array of that object: [User] How can I map this array even if it has not a specific field name? This is what I did:

class Users: Mappable {

var users: [User]?

required init?(map: Map) {

        }

// Mappable
        func mapping(map: Map) {
          //What have I to put here??
        }

}

1
  • please before down voting explain why Commented Dec 5, 2017 at 15:25

1 Answer 1

2

It's quite simple you just have to use mapArray instead of map.

You just don't need to create new users class, only user class is enough to do this task

Use in this way

let arrUser = Mapper<User>().mapArray(JSONObject: JSONResponse.rawValue)
Sign up to request clarification or add additional context in comments.

2 Comments

I got this error: Use of unresolved identifier 'JSONResponse' I'm using swift 4
this is the way i get my data.... you can use your data . The array you are getting

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.