gpt4 book ai didi

ios - 如何在 AlamofireObjectMapper 中映射对象

转载 作者:行者123 更新时间:2023-11-29 01:02:18 25 4
gpt4 key购买 nike

在我的应用程序中,我使用 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 它的工作,但我想从 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 方法中打印移动值。那我该怎么做呢?

最佳答案

由于 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 创建单独的类,并可以以相同的方式使用。

关于ios - 如何在 AlamofireObjectMapper 中映射对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36864547/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com