在我的应用程序中,我使用alamofire对象映射器进行对象映射。 现在这是我的代码
class OrderDetail: Mappable {
var Message : String!
var Status : Int!
var result:[OrderDetailData]?
required init?(_ map: Map) {
}
func mapping(map: Map) {
Message <- map["Message"]
Status <- map["Status"]
result <- map["Result"]
}
}
class OrderDetailData: Mappable {
var statusId: String?
var tax: String?
var total: String?
var toTime: String?
var updated: String?
var userId: String?
var orederDetail : [OrderDetailSecond]?
required init?(_ map: Map){
}
func mapping(map: Map) {
statusId <- map["status_id"]
tax <- map["tax"]
total <- map["total"]
toTime <- map["totime"]
updated <- map["updated"]
userId <- map["user_id"]
orederDetail <- map["F017A_order_detail"]
}
}
class OrderDetailSecond: Mappable {
var id : String?
var isRxMedicine : Int?
var medicineTypeId : String?
var name : String?
var orderId : String?
var price : String?
var quentity : String?
var strength : String?
required init?(_ map: Map){
}
func mapping(map: Map) {
id <- map["id"]
isRxMedicine <- map["is_rx_medicine"]
medicineTypeId <- map["medicine_type_id"]
name <- map["name"]
orderId <- map["order_id"]
price <- map["price"]
quentity <- map["qty"]
strength <- map["strengh"]
}
}
这就是我在做映射的方式。 这是我的api电话
class DataControllerOrderDetail: UIViewController {
func get_order_detail(orderId : String,completion : (orderDetailObject : OrderDetail) -> Void) {
let url = "\(OrderURL)full_order_data"
let param : [String : AnyObject] = [
"orderId" : orderId
]
Alamofire.request(.GET, url, parameters: param, encoding: .URL).responseObject { (response:Response<OrderDetail, NSError>) in
switch response.result{
case .Success(let value) :
var my_order_detail : OrderDetail?
my_order_detail = value
completion(orderDetailObject: my_order_detail!)
case.Failure(let error) : break
}
}
}
}
现在为orderDetailSecond类元素的访问值我正在执行此代码
override func viewDidLoad() {
DataControllerObject.get_order_detail(order_id) { (orderDetailObject) in
self.my_order_list = orderDetailObject
if let orderdetail = self.my_order_list, orderDetailDataObject = orderdetail.result {
let detailArray = orderDetailDataObject[0].orederDetail
if let orderdata = detailArray
{
print(orderdata.count)
}
}
}
}
每次都必须访问orderDetailSecond
类元素值。我怎么能映射所以我可以直接访问像。 orderDetailSecond.elementname
答案 0 :(得分:1)
If you don't need the separation, you could delete the OrderDetailSecond class and add its properties to OrderDetailData using nested mapping:
class OrderDetailData: Mappable {
var statusId: String?
var tax: String?
var total: String?
var toTime: String?
var updated: String?
var userId: String?
//OrderDetailSecond elements
var id : String?
var isRxMedicine : Int?
var medicineTypeId : String?
var name : String?
var orderId : String?
var price : String?
var quentity : String?
var strength : String?
required init?(_ map: Map){
}
func mapping(map: Map) {
statusId <- map["status_id"]
tax <- map["tax"]
total <- map["total"]
toTime <- map["totime"]
updated <- map["updated"]
userId <- map["user_id"]
orederDetail <- map["F017A_order_detail"]
//OrderDetailSecond elements
id <- map["F017A_order_detail.id"]
isRxMedicine <- map["F017A_order_detail.is_rx_medicine"]
medicineTypeId <- map["F017A_order_detail.medicine_type_id"]
name <- map["F017A_order_detail.name"]
orderId <- map["F017A_order_detail.order_id"]
price <- map["F017A_order_detail.price"]
quentity <- map["F017A_order_detail.qty"]
strength <- map["F017A_order_detail.strengh"]
}
}
Source: https://github.com/Hearst-DD/ObjectMapper#easy-mapping-of-nested-objects