如何在Alamofire中将数据传递给字典

时间:2019-04-03 18:51:58

标签: swift swift3

我的json到字典的响应数组,但是我想在show tableview.my中获取我的数据,但是我在一个键“ member”中的响应,我想获取一个key的数据,但出现了一些错误。

我正在尝试将数组作为参数传递给Alamofier请求。但是我没有从json中获取数据,但是我的代码却出现错误,我无法执行此操作。...

我的网址:

https://myfoodtalk.com:3001/api/restaurants?filter={"counts":"restaurant-comments","order":"created DESC","include":[{"relation":"follow-restaurants","scope":{"where":{"m_id":""}}},{"relation":"members"},{"relation":"favorite-restaurants","scope":{"where":{"m_id":""}}}]}

我的模型班:

class RestaurantsData: NSObject {
    var title = String()
    var descriptions = String()
    var image1 = String()
    var postcommentsCount = String()
    var viewscount = String()
    var created = String()
    var members = [String:Any]()
    var mbersdata = membersData()
    func getRestaurentData(dataArray: [[String:Any]]) -> [RestaurantsData] {
        var array = [RestaurantsData]()
        let mObj = membersData()

        for item in dataArray {
            let obj = RestaurantsData()
            obj.title = item.validatedValue("title", expected: String() as AnyObject) as! String
            obj.descriptions = item.validatedValue("description", expected: String() as AnyObject) as! String
            obj.image1 = item.validatedValue("image1", expected: String() as AnyObject) as! String
            obj.postcommentsCount = item.validatedValue("post-commentsCount", expected: String() as AnyObject) as! String
            obj.viewscount = item.validatedValue("views_count", expected: String() as AnyObject) as! String
            obj.created = item.validatedValue("created", expected: String() as AnyObject) as! String
            obj.members = item.validatedValue("members", expected: [String:Any]() as AnyObject) as! [String:Any]
            obj.mbersdata =   mObj.getMemberData(dataDic:obj.members)
            array.append(obj)
        }
        return array
    }
    //Mark:- Class created for member data

    class membersData: NSObject {
        var photo = String()
        var created = String()
        var username = String()
        func getMemberData(dataDic: [String:Any]) -> membersData {
            let obj = membersData()

            obj.username = dataDic.validatedValue("username", expected: String() as AnyObject) as! String
            obj.created = dataDic.validatedValue("created", expected: String() as AnyObject) as! String
            obj.photo = dataDic.validatedValue("photo", expected: String() as AnyObject) as! String

            return obj

        }
    }
}

我的代码:-我正在使用Alamofire

 var dataArray = [RestaurantsData]()

   //MARK: Web API calling
   func ShowRestaurantsData(){

      var params = [String:Any]
            params = [
                "counts":"restaurant-comments",
                "order":"created DESC",
                "include":[["relation":"follow-restaurants","scope":["where":["m_id":""]]],["relation":"members"],["relation":"favorite-restaurants","scope":["where":["m_id":""]]]

      Alamofire.request( "https://myfoodtalk.com:3001/api/restaurants", method:.get, parameters: nil, headers: nil).responseJSON { (responseObject) -> Void in
                switch responseObject.result
                {
                case .success(let value):
                    let dataArray = value as! [[String:Any]]
                    print("JSON Response:::::: \(dataArray)")

                    let obj = RestaurantsData()
                    self.dataArray = obj.getRestaurentData(dataArray: dataArray)
                    self.tableView.reloadData()

                case .failure(let error):
                    print(error)
                }
            }

        }

1 个答案:

答案 0 :(得分:1)

在您的请求中

Alamofire.request( "https://myfoodtalk.com:3001/api/restaurants", method:.get, parameters: nil, headers: nil).responseJSON { (responseObject) -> Void in

您正在通过“

  

参数为零

这是错误的,请传递“ params” ...