Ios Swift-额外参数';图像';随时待命
我正试图改变以下的工作Ios Swift-额外参数';图像';随时待命,ios,swift,arguments,Ios,Swift,Arguments,我正试图改变以下的工作 tempNews.append(News(title: $0.title)) 进入 但是,当我键入它时,我在call中得到了以下错误额外参数'image'现在JSON文件确实有image,因此我知道导致此错误的不是JSON返回。所以它一定是另外的东西,或者 struct NewsData: Decodable{ let news: [articalData] } struct articalData: Decodable{ let title: Str
tempNews.append(News(title: $0.title))
进入
但是,当我键入它时,我在call中得到了以下错误额外参数'image'
现在JSON文件确实有image,因此我知道导致此错误的不是JSON返回。所以它一定是另外的东西,或者
struct NewsData: Decodable{
let news: [articalData]
}
struct articalData: Decodable{
let title: String
let image: String
}
或
这是完整视图控制器脚本
//
// 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)
}
}
}
我的第一个计划是让它不抛出那个错误,一旦我有了这个工作,我将使用翠鸟加载远程图像
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
}
我尝试编译您的代码并成功运行:tempNews.append(articalData(title:$0.title,image:$0.image)),其中tempNews是var tempNews:[articalData]=[],函数签名是func newsfetch(u-completionHandler:@escaping([articalData]?)->Void){
class News {
// var image: UIImage
var title: String
var image: String
init(title: String){
self.image = image
self.title = title
}
}
init(title: String,
image: String) {
self.image = image
self.title = title
}