如何解决此错误Optional(Alamofire.AFError.invalidURL)?

时间:2019-02-08 00:46:02

标签: ios swift cocoapods alamofire

当我将我的应用程序链接到url时,出现此错误

  

可选(Alamofire.AFError.invalidURL)

import UIKit
import Alamofire
import SwiftyJSON

class MainViewController: UIViewController , UITableViewDataSource , UITableViewDelegate {

  @IBOutlet weak var tableView: UITableView?

  var foods :[[String: Any]] = [[String: Any]]()

  // let foodsUrl : String = "​​https://api.myjson.com/bins/1bnsyj"

  override func viewDidLoad() {
    super.viewDidLoad()


    Alamofire.request("​​https://api.myjson.com/bins/1bnsyj",method : .get).responseJSON { (response) in
      if let responseValue = response.result.value as! [String: Any]? {
        if let responseFoods = responseValue["items"] as! [[String: Any]]? {

          self.foods = responseFoods
          self.tableView?.reloadData()

        }
      }
      else {
        print("error : \(String(describing: response.result.error))")
      }
    }
  }

  //Mark - UITableViewDataSource & UITableViewDelegate

  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 14
  }

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

    if self.foods.count > 0 {
      let  eachFood = foods[indexPath.row]
      cell.lblFoodName?.text = (eachFood["name"] as? String) ?? ""
      cell.lblFoodDescription?.text = (eachFood["description"] as? String) ?? ""
    }

    return cell
  }
}

0 个答案:

没有答案