2016-04-26 93 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; 
} 

现在这是我的代码

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

} 

}

做了一个类通过这段代码,我可以得到消息,但现在我想映射结果对象,所以我该如何做到这一点?

感谢俞拉吉双曲正弦它的工作,但我要访问从结果对象中的所有变量,所以我让这个对象

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 protected]”;从数据中获取字符串类型,如果让emailID = result [“email”]为?字符串{} –

+0

为“Id”例如//如果让idValue = result [“id”] as?诠释{///打印(idValue)} –

1

正如从结果参数API响应表示应该使用Dictionary映射的另一个JSON对象。您可以使用以下内容替换当前的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"] 
     } 
    }