0

在我的应用程序中,我使用 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

4

1 回答 1

1

如果您不需要分离,您可以删除 OrderDetailSecond 类并使用嵌套映射将其属性添加到 OrderDetailData:

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

来源:https ://github.com/Hearst-DD/ObjectMapper#easy-mapping-of-nested-objects

于 2017-06-15T10:28:36.337 回答