0

Here is my model

class ResponseDataType: Mappable {

    var status: Int?
    var message: String?
    var info: [Info]?

    required init?(map: Map) { }

    func mapping(map: Map) {
        status <- map["status"]
        message <- map["message"]
        info <- map["member_info"]
    }
}

Here is my JSON

"status": 200,
    "data": {
        "member_info": [
            {
                "fullname": "werwerwer",
                "type": "werwer",
                "profile_image": "sdfsdfsd.jpg",
                "email": "wfwe@werwegt",
                "contact": ""
            }
        ]
    },
    "message": "Login Success"
}

Im having a hard time mapping the array inside the data. Please tell me what is wrong with my code.

1
  • 3
    Is there a good reason for not using Codable? Commented Feb 28, 2020 at 20:17

2 Answers 2

1

You forgot the data. It should be like this:

class ResponseDataType: Mappable {

var status: Int?
var message: String?
var data: Data?

required init?(map: Map) { }

func mapping(map: Map) {
    status <- map["status"]
    message <- map["message"]
    data <- map["data"]
}

and your data class:

class Data: Mappable {

var info: [Info]?

required init?(map: Map) { }

func mapping(map: Map) {
    info <- map["member_info"]
}
Sign up to request clarification or add additional context in comments.

Comments

0

If your Info object conforms to Mappable, everything should work properly in your code. But try to read about Codable protocol, it’s much easier to map objects with it!

Comments

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.