【发布时间】:2021-07-08 13:13:34
【问题描述】:
我正在做一个新闻管理练习。我有一些课程:
- 作者
- 新闻(父亲)
- NewsVideo(子)(该类与父类具有相同的属性,另外还有两个属性)
- 新闻管理
从 NewsManagement 类中,我必须创建一个插入新闻的函数。 我已经创建了一个新闻类型的空数组。 NewsManagement 类只有一个属性,一个空的 News 数组。
var newsCollection: [News] = []
class NewsManagement {
var news: [News] = []
init(news: [News]) {
self.news = news
}
func insertNews(title: String, text: String, date: Int, author: Autore, urlVideo: String?, videoLength: Double?) -> String? {
//these are the properties of the newsVideo class
guard let urlVideo = urlVideo, let videoLength = videoLength else {
return nil
}
self.news.append(News.init(title: String, text: String, date: Int, author: Author))
return "news addition”
}
}
var manager = NewsManagement.init(news: newsCollection)
manager.insertNews(title: "prima prova", text: "spero funzioni", date: 13, author: authors[3], urlVideo: "www.prova.it", videoLength: 4.5)
//authors is an array of instance of class Author
不幸的是,它不起作用。 你知道错误在哪里吗? 或者你知道另一种方式吗? 我已经尝试了很长时间的解决方案,但它们都不起作用
谢谢!
【问题讨论】:
-
为什么 NewsVideo 是 News 的子类?您不能只将 NewsVideo 作为 News 中的可选属性吗?
-
当你说它不工作时,你是说当你输入视频时它什么都不做?
-
在这一行 News.init(title: String, text: String, date: Int, author: Author) 你没有向构造函数发送参数。看来这是一个编辑器占位符。尝试像 News.init(title: title, text: text, date: date, author: author) 一样,还有,为什么你需要 var newsCollection: [News] = [] 这一行?