2016-04-26 10 views
0

私のアプリケーションでは、マッピングにAlamofireObjectMapperを使用しています。初めて私はこれを使用しています。ここで私はAPIから取得しています私の応答があるAlamofireObjectMapperでオブジェクトをマップする方法

{ 
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; 
} 

は今、これはこれは私がマッピング

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


required init?(_ map: Map){ 

} 

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

} 

}

のために作られたクラスで、私のコード

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) 



      } 

ですこのコードで私はMessageを得ることができますが、今私はResultオブジェクトをマップしたいので、どうすればいいですか?

おかげユブラージシンその作業しかし、私は、このオブジェクト

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方法でモバイル値を印刷したくなるので、結果オブジェクトからすべての変数にアクセスしたいです。どうすればいいのですか?

答えて

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

は//ここにあなたが任意のキーと値のペアを取得することができ、この辞書を使用することによって、結果 で辞書を取得 //そのだっただけで、通常の解析では、それはからあなたの結果パラメータとして

+0

おかげでその作業をバディが、その私に、オブジェクト全体を与えると信じてすることが、私には、ID、パスワードなどのオブジェクトのすべての変数にアクセスするには、メールをしたいですこれを行う?? –

+0

オブジェクトを取得すると、必要な値に対して同じことを再度実行します。例:email = "[email protected]";データから文字列型の場合は、emailID = result ["email"]としますか? String {} –

+0

"Id"の場合// //もしidValue = result ["id"]を? Int {/// print(idValue)} –

1

を働くことを願っていますAPIレスポンスは、別の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のために別のクラスを作成することができますし、同じように使用することができます。そうサンザシは、私ができるあなたが持っている

+0

私の質問を参照してください私は私の質問を更新します。 –

0

はちょうど他のマッピング可能なクラスと呼ばれる結果

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"] 
     } 
    } 
関連する問題