ios - Swift - 调用中的额外参数 'image'

标签 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
    }
}

这是完整的 View Controller 脚本
    //
//  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 加载远程图像。

最佳答案

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
}

关于ios - Swift - 调用中的额外参数 'image',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59101526/

相关文章:

ios - UISearchController的错位

ios - swift ,线程 1 : EXC_BAD_INSTRUCTION error

javascript - 为什么 JavaScript 参数有 length 属性而其他对象没有?

java - Java/OOP 编程中传递参数、好的和坏的实践

ios - 错误 ITMS-90514 : "Missing Code Signing Entitlement. Use of the ' network-authentication' background mode in 'Payload/xxx.app/xxx'

ios - 将数据从子模态 VC 传递到父 View Controller 的最佳方式?

iphone - 仅使用 Core Graphics 和 CADisplayLink 的图像动画示例

swift - 在 Swift 中 5 秒后跳出 while 循环

ios - 带有贝塞尔路径的基本 UIImageView 蒙版形状

callback - AutoIt _Timer_SetTimer - 附加回调参数