我是Swift的新手,正在使用ObjectMapper解析JSON,但我希望在TableView中显示数据。但是下载图片有问题
我使用扩展UIImageView func downloadFrom

我的问题 :


  无效的重新声明“ download From(url:ContentMode :)”


我的代码:

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

        let strUrl = categoty[indexPath.row].picture

        cell.titleCategory.text = self.categoty[indexPath.row].title

        cell.imageCategory.downloadFrom(url: URL(string: strUrl!)!)

        return cell
    }
}

extension UIImageView {
    func downloadFrom(url: URL, contentMode mode: UIViewContentMode = .scaleAspectFit) {
        contentMode = mode
        URLSession.shared.dataTask(with: url) { (data, response, error) in
            guard
                let httpURLResponse = response as? HTTPURLResponse, httpURLResponse.statusCode == 200,
                let mimeType = response?.mimeType, mimeType.hasPrefix("image"),
                let data = data, error == nil,
                let image = UIImage(data: data)
                else { return }
            DispatchQueue.main.async() { () -> Void in
                self.image = image
            }
            }.resume()
    }
}


ios - 下载图片时出错-LMLPHP

最佳答案

该错误表示您在代码中两次用相同的名称编写了相同的函数。

在您的代码中寻找此方法的重复实现:

func downloadFrom(url: URL, contentMode mode: UIViewContentMode = .scaleAspectFit) {

10-02 09:07