Jack.Right
Jack.Right

Reputation: 994

How to map object in AlamofireObjectMapper

in my app i am using AlamofireObjectMapper for mapping. first time i am using this. here is my response which i am getting from API

    {
Message = "email verification link has been sent to your email. please verify your account.";
Result =     {
"V002_vendors_type" = "<null>";
"V003_pharmacy" = ();
"V010_subscription" = "<null>";
"attempt_date" = "<null>";
created = "2016-04-26T11:07:30.3192745+00:00";
email = "[email protected]";
"first_name" = abc;
id = 10167;
"is_lock" = 0;
"last_name" = "<null>";
mobile = 9999999999;
password = xxxxxxxxx;
"profile_Image" = "<null>";
status = PV;
subscription = 1;
updated = "2016-04-26T11:07:30.3192745+00:00";
"Fix_id" = 1;
};
Status = 1;
}

now this is my code

func pharmacySignUp()
        {
            let url = "http://\(basicURL)vendor_signup"
            let param :[String : AnyObject] =
                [
                    "email"      : txtemail.text!,
                    "password"   : txtpassword.text!,
                    "mobile"     : txtmobile.text!,
                    "first_name" : txtname.text!
              ]

            Alamofire.request(.POST, url, parameters: param, encoding: .JSON).responseObject { (response:Response<signupVarificationCode, NSError>) in
                print(response.result.value)
                let signupVarificationCode = response.result.value
                print(signupVarificationCode)
                print(signupVarificationCode!.Message)
                print(signupVarificationCode?.status)



            }

this is a class which i made for mapping

class signupVarificationCode: Mappable {
var Message : String?
var status : String?


required init?(_ map: Map){

}

func mapping(map: Map) {
    Message <- map["Message"]
    status <- map["Status"]

}

}

by this code i can get Message but now i want to map Result object so how can i do this?

Thanks Yuvraj Sinh its working but i want to access all variable from Result object so i make this object

class Result: Mappable {
var lastName : String?
var mobile : String?
var id: String?

required init?(_ map: Map){

}

func mapping(map: Map) {
    lastName <- map["last_name"]
    mobile <- map["mobile"]
    id <- map["id"]
}

}

i want to print mobile value in my pharmacySignup method. so how can i do this?

Upvotes: 0

Views: 579

Answers (3)

Alejandro
Alejandro

Reputation: 325

You have to just believe other mappable class called Result

    class signupVarificationCode: Mappable {
     var Message : String?
     var status : String?
     var result : Result?

     required init?(_ map: Map){

     }

     func mapping(map: Map) {
         Message <- map["Message"]
         status <- map["Status"]
         result <- map["Result"]
     }
}

class Result: Mappable {
       var mobile: String?

    required init?(_ map: Map){

         }

         func mapping(map: Map) {
             //Mapping attributtes for example
             mobile <- map["mobile"]
         }
    }

Upvotes: 0

var result:[String:AnyObject] = map[“Result”] as! [String:AnyObject]

// here you get the dictionary in result by using this dict you can get any key value pair // its was just a normal parsing hope it will work

Upvotes: 0

Yuvrajsinh
Yuvrajsinh

Reputation: 4584

As your Result parameter from API response represents another JSON object it should be mapped with Dictionary. You can replace your current signupVarificationCode with following.

class signupVarificationCode: Mappable {
     var Message : String?
     var status : String?
     var result : [String:AnyObject]?

     required init?(_ map: Map){

     }

     func mapping(map: Map) {
         Message <- map["Message"]
         status <- map["Status"]
         result <- map["Result"] as! [String:AnyObject]
     }
}

If you want to go more Object Oriented then you can create separate class for Result and can use in same way.

Upvotes: 1

Related Questions