Swift-通话中有额外的参数“ image”

时间:2019-11-29 08:51:15

标签: ios swift arguments

我正在尝试以下可行的方法

tempNews.append(News(title: $0.title))

进入

tempNews.append(News(title: $0.title, image: $0.image))

但是,当我键入它时,我得到以下错误Extra argument 'image' in call,现在JSON文件中确实有图像,因此我知道它不是导致此错误的JSON返回。

struct NewsData: Decodable{
    let news: [articalData]
}

struct articalData: Decodable{
    let title: String
    let image: String
}

import Foundation
//import UIKit

class News {
  //  var image: UIImage
    var title: String
    var image: String

    init(title: String) {
        self.image = image
        self.title = title
    }
}

这是完整视图控制器脚本

    //
//  NewsViewController.swift
//  DRN1
//
//  Created by Russell Harrower on 26/11/19.
//  Copyright © 2019 Russell Harrower. All rights reserved.
//

import UIKit
import Foundation


struct NewsData: Decodable{
    let news: [articalData]
}

struct articalData: Decodable{
    let title: String
    let image: String
}

class NewsViewController: UIViewController {
    @IBOutlet weak var tableView: UITableView!


    var news: [News] = []

    override func viewDidLoad() {
      super.viewDidLoad()

        tableView.delegate = self
        tableView.dataSource = self
    }

    override func viewDidAppear(_ animated: Bool) {
       self.tabBarController?.navigationItem.title = "News"

         self.newsfetch { [weak self] news in
                    guard let news = news else { return }
                    self?.news = news
            DispatchQueue.main.async {
                    self?.tableView.reloadData()
            }
            }
     }


    func newsfetch(_ completionHandler:  @escaping ([News]?)->Void){
        let jsonURLString = "https://api.drn1.com.au/api-access/news"
        guard let feedurl = URL(string: jsonURLString) else {  return }

        URLSession.shared.dataTask(with: feedurl){ (data,response,err)
            in
            guard let news = data else { return }
            do {
                let newsdata = try JSONDecoder().decode(NewsData.self, from: news)
                var tempNews: [News] = []
                newsdata.news.forEach(){
                    var strUrl = $0.image
                    strUrl = strUrl.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!

                    tempNews.append(News(title: $0.title, image: strUrl))
                }
                completionHandler(tempNews)
            } catch let jsonErr {
                print("error json ", jsonErr)
                completionHandler(nil)
            }
        }.resume()

    }
}


extension NewsViewController: UITableViewDataSource, UITableViewDelegate {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return news.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let newsa = news[indexPath.row]

        let cell = tableView.dequeueReusableCell(withIdentifier: "NewsCell") as! NewsCell
        cell.setNews(news: newsa)
        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        performSegue(withIdentifier: "shownewsarticle", sender: self)
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if let destination = segue.destination as?  NewsArticleViewController{
            destination.article = news[(tableView.indexPathForSelectedRow?.row)!]
            tableView.deselectRow(at: tableView.indexPathForSelectedRow!, animated: true)
        }

    }


}

我的第一个计划是使它不引发该错误,一旦完成该工作,我将使用Kingfisher加载远程图像。

1 个答案:

答案 0 :(得分:2)

class News {
  //  var image: UIImage
    var title: String
    var image: String
    init(title: String){
        self.image = image
        self.title = title
    }
}

init方法不包含图像。这是你的问题

更改为

init(title: String,
     image: String) {
    self.image = image
    self.title = title
}