Вставить информацию из REST API в табличное представление - PullRequest
0 голосов
/ 19 марта 2019

После того, как я задал еще один вопрос сегодня и исправил код Swift 4.2, я понял, что не могу отладить приложение из-за некоторых профилей.

Ошибки, отображаемые при тестировании приложения

Приложение также аварийно завершает работу при запуске, поэтому я пока ничего не могу сделать.Я считаю, что проблема заключается в том, как я получаю информацию с веб-сервера (в настоящее время мой компьютер).Я новичок в этом, поэтому у меня могут быть некоторые ошибки в моем коде, так что терпите меня.

import UIKit

class InfoTableViewController: UITableViewController {

    //MARK: Properties
    class Produs {
        var nume: String!
        var codBare: String!

        init(nume: String, codBare: String) {
            self.nume = nume
            self.codBare = codBare
        }
    }

    var produse = [Produs]()

    override func viewDidLoad() {
        super.viewDidLoad()

        //Load elements from server, theoretically
        loadProducts()
    }

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return produse.count
    }


    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        // Table view cells are reused and should be dequeued using a cell identifier.
        let cellIdentifier = "InfoTableViewCell"

        guard let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? InfoTableViewCell  else {
            fatalError("The dequeued cell is not an instance of InfoTableViewCell.")
        }

        // Fetches the appropriate meal for the data source layout.
        let produs = produse[indexPath.row]

        cell.nameMain.text = produs.nume
        cell.nameInfo.text = produs.codBare

        return cell
    }
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }

    //MARK: Private Methods
    private func loadProducts() { //This function retrieves information in a JSON format from the server
        var request = URLRequest(url: URL(string: "192.168.0.145")!)
        request.httpMethod = "GET"

        URLSession.shared.dataTask(with: request, completionHandler: { data, response, error -> Void in
            do {
                let decoder = JSONDecoder()
                let json = try decoder.decode([[String]].self, from: data!)
                print(json)
                for produs in json {
                    print(produs)
                    var nume_prod: String = produs[0] // Numele produsului
                    var cod_ext: String = produs[1]   // Codul de bare al produsului
                    var prod_obj = Produs(nume: nume_prod, codBare: cod_ext)
                    self.produse.append(prod_obj)
                }
            } catch {
                print("JSON Serialization error")
            }
        }).resume()
    }
}

Я получаю информацию с сервера в файле JSON, который представляет собой массив массивов ивыглядит так:

[
  [
    "product1",
    "code1"
  ],
  [
    "product2",
    "code2"
  ],
  [
    "product3",
    "code3"
  ]
]

Спасибо за помощь!

1 Ответ

1 голос
/ 19 марта 2019

Не отправляйте JSON как массив массивов, отправляйте его как массив словарей. Это значительно упрощает декодирование.

[
  {"name":"product1", "code":"code1"},
  {"name":"product2", "code":"code2"},
  {"name":"product3", "code":"code3"}
]

Затем объявляйте модель как struct и никогда не объявляйте свойства как неявные необязательные необязательные, которые инициализируются не необязательными значениями . Если вам нужны дополнительные параметры, объявите их как обычные дополнительные (?), в противном случае необязательные

struct Produs {
    let name: String
    let code: String
}

Заменить loadProducts на

private func loadProducts() { //This function retrieves information in a JSON format from the server
    let url = URL(string: "192.168.0.145")!

    URLSession.shared.dataTask(with: url, completionHandler: {[unowned self] data, response, error in
        if let error = error { print(error); return }
        do {
            self.produse = try JSONDecoder().decode([Produs].self, from: data!)
            print(self.produse)
            DispatchQueue.main.async {
                self.tableView.reloadData()
            }
        } catch {
            print(error)
        }
    }).resume()
}

И заменить cellForRowAt на

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cellIdentifier = "InfoTableViewCell"
    let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! InfoTableViewCell            

    // Fetches the appropriate meal for the data source layout.
    let produs = produse[indexPath.row]

    cell.nameMain.text = produs.name
    cell.nameInfo.text = produs.code

    return cell
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...