ios - 我如何访问其他 Mappble 类对象的值。(Alamofire Object Mapper)

标签 ios swift mapping alamofire objectmapper

在我的应用程序中,我是第一次使用 AlamofireObjectMapper。 所以我在一个类中映射 api 响应数据,然后我想使用该数据。 所以这是我如何映射对象的代码

    extension OrderListViewController
{
 func get_order_list()
  {


        let url = "\(OrderURL)get_New_order_byPharmacy"
        let param : [String : AnyObject] = [

            "pharmacyId" : "131"
        ]

        Alamofire.request(.GET, url, parameters: param, encoding: .URL).responseObject { (response:Response<OrderList, NSError>) in
            let OrderList = response.result.value
            print(OrderList!.Message)
        }

    }
}

这是我保存数据的类

    class OrderList: Mappable {
    var Message : String!
    var Status : Int!
    var result:[OrderResult]?



    required init?(_ map: Map){

    }

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

    }

}

现在在我的 OrderListViewController 中,我想使用这些数据,那么我该如何使用呢??

    class OrderListViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {

    @IBOutlet weak var table_OrderList: UITableView!


    override func viewDidLoad() {
        slideMenuController()?.addLeftBarButtonWithImage(UIImage(named: "ic_menu_black_24dp")!)
        slideMenuController()?.addRightBarButtonWithImage(UIImage(named: "ic_notifications_black_24dp")!)
        get_order_list()
    }


    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell : OrderList_Cell = tableView.dequeueReusableCellWithIdentifier("OrderList_Cell") as! OrderList_Cell



        return cell

    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 20

    }
}

例如,我想在我的表格 View 单元格标签中打印消息值。那么如何从 OrderList 中获取该值?

感谢 slava,它给了我一些解决方案。但我的 json 响应给了我数组。那么我该如何管理呢?我想返回 numberofrowinSetcion 是数组的计数,所以我该怎么做。请查看我更新的问题。

这是我的 api 响应。

    {
    "Status": 1,
    "Message": "records are available",
    "Result": [
        {
            "id": 30162,
            "status_id": 2,
            "status_type": "New Order",
            "created_date": "2016-05-11T10:45:00.6779848",
            "created": "11 May 2016"
        },
        {
            "id": 30170,
            "status_id": 2,
            "status_type": "New Order",
            "created_date": "2016-05-12T07:01:00.6968385",
            "created": "12 May 2016"
        },
        {
            "id": 30171,
            "status_id": 2,
            "status_type": "New Order",
            "created_date": "2016-05-12T09:12:53.5538349",
            "created": "12 May 2016"
        },
        {
            "id": 30172,
            "status_id": 2,
            "status_type": "New Order",
            "created_date": "2016-05-12T09:46:09.4329398",
            "created": "12 May 2016"
        },
        {
            "id": 30173,
            "status_id": 2,
            "status_type": "New Order",
            "created_date": "2016-05-12T11:26:58.3211678",
            "created": "12 May 2016"
        },
        {
            "id": 30178,
            "status_id": 2,
            "status_type": "New Order",
            "created_date": "2016-05-16T07:34:19.9128517",
            "created": "16 May 2016"
        }
    ]
}

最佳答案

您需要在 Controller 中使用一个局部变量来存储所有接收到的用于填充表格的信息。应该做这样的事情:

class OrderListViewController: ... {
    private var orderList: OrderList? // <- the local variable needed
    ...
}

extension OrderListViewController {
    func get_order_list() {
        ...
        Alamofire
            .request(...)
            .responseObject { (response:Response<OrderList, NSError>) in
                switch response.result {
                case .Success(let value):
                    self.orderList = value // <- fill the local variable with the loaded data
                    self.tableView.reloadData()
                case .Failure(let error):
                    // handle error
                }
            }
    }
    ...
}

extension OrderListViewController: UITableViewDataSource {
    ...
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell : OrderList_Cell = tableView.dequeueReusableCellWithIdentifier("OrderList_Cell") as! OrderList_Cell
        // I assume 'OrderList_Cell' class has outlet for status type named 'statusTypeLabel' and OrderResult.statusType is of type String
        if let orderList = orderList, orderResults = orderList.result {
            cell.statusTypeLabel.text = orderResults[indexPath.row].statusType // <- use of the locally stored data
        }

        return cell
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if let orderList = orderList, orderResults = orderList.result {
            return orderResults.count
        } else {
            return 0
        }
    }
}

注意:如果您从后端收到 JSON 中的单个对象,代码应该是正确的。 如果后端发送对象数组 - 您需要使用数组来存储本地数据(private var listOfOrderLists: [OrderList])并使用 Alamofire.request(...).responseArray (...) 代替。但是关于局部变量的想法仍然是一样的。

关于ios - 我如何访问其他 Mappble 类对象的值。(Alamofire Object Mapper),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37267940/

相关文章:

ios - 为什么 UITableView 的最后一个单元格的高度用于剩余的空单元格?

ios - gdb 调用 malloc 失败 (iOS)

ios - Swift(iOS) 一个 View Controller 中的两个 ScrollView

ios - QuickBlox iOS SDK,启用云后端服务器 AUTHKEY/APPLICATION ID 到 APP DELEGATE

ios - NSMutableAttributedString paragraphStyle 不适用

elasticsearch - 如何删除 Elasticsearch 中的字段映射

ios - 是否可以将Unity与UIKit混合使用?

ios - Apple Watch 站立检测

c# - 一对多关系映射返回验证错误

matlab - 使用多变量多项式的特征映射