SwiftyJson массив в массиве - PullRequest
0 голосов
/ 15 мая 2018

Я анализирую JSON с сервера с помощью Alamofire и SwiftyJson. Я показываю это в табличном представлении, а затем, когда нажимаю на строку, открываю ссылку в Интернете.

Но у меня проблемы с анализом массива.

array`[
    {
        "title": " ",
        "likes": 0,
        "post_id": 60050,
        "image": "",
        "image1": "",
        "image2": "",
        "data": "",
        "arguments": [
            "link",
            "url"
        ],
        "provider": "custom",
        "price": " usd",
        "desc": "",
        "shop": ""
    },`

Мой запрос:

Alamofire.request("").responseJSON(completionHandler: { response in
    if ((response.result.value) != nil) {
        let swifts = JSON(response.result.value!)

        if let resData = swifts.arrayObject{
        }
        self.dataTable = resData as! [[String:AnyObject]]
    }

Это чтобы показать текст:

cell.titleLabel.text = indexData["arguments "] as? String

Но при загрузке у меня появляется ошибка:

Swift._SwiftDeferredNSArray 0x600000228cc0>( url

Ответы [ 2 ]

0 голосов
/ 17 мая 2018

Здесь я предполагаю, что вы отлично получаете ответ от API. Теперь проанализируйте данные следующим образом.

  1. Создать глобальную переменную var arrData : [Any] = []

  2. Теперь вызовите API и получите такие данные.

    if ((response.result.value) != nil) {
        self.arrData = (response.result.value) // here your all response in arrData. 
        self.tblView.reloadData() // reload tableview
    }
    else{
        // you didn't get the data
    }        
    
  3. Теперь в TableView DataSource

    • В numberOfRowsInSection

      func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
         return arrData.count 
      }
      
    • В cellForRow

      func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
          let cell = tableView.dequeueReusableCell(withIdentifier: "your_cell_id") as! YourCell
      
          let aDictValue = arrData[indexPath.row] as? [String : Any]
      
          cell.labelTitle.text = aDictValue["title"] as! String
          cell.labelProvider.text = aDictValue["provider"] as! String
      
          // set your data like this.
      
          return cell
      }
      

Если проблема не устранена, вы можете задать ее.

0 голосов
/ 15 мая 2018

Попробуй разобрать вот так

let array = swifts["arguments"].arrayValue 

for item in array 
{
    print(item)

}
...