0

私のアプリでは、マッピングに AlamofireObjectMapper を使用しています。初めてこれを使用しています。ここに私が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 = "abc@gmail.com";
"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;
}

これが私のコードです

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)



            }

これは、マッピング用に作成したクラスです

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


required init?(_ map: Map){

}

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

}

}

このコードでメッセージを取得できますが、結果オブジェクトをマップしたいので、どうすればよいですか?

Yuvraj Sinhに感謝しますが、結果オブジェクトからすべての変数にアクセスしたいので、このオブジェクトを作成します

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"]
}

}

pharmacySignup メソッドでモバイル値を出力したいと考えています。どうすればこれを行うことができますか?

4

3 に答える 3

1

API 応答からの Result パラメーターは別の JSON オブジェクトを表すため、Dictionary にマップする必要があります。現在の signupVarificationCode を次のものに置き換えることができます。

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]
     }
}

さらにオブジェクト指向にしたい場合は、別のクラスを作成Resultして同じように使用できます。

于 2016-04-26T12:33:03.067 に答える
0

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"]
         }
    }
于 2016-04-27T10:33:59.813 に答える