【发布时间】:2019-07-27 17:11:49
【问题描述】:
我正在使用 Swift UI 创建一个新闻应用程序,并使用 firebase。但我总是有同样的错误:“在调用中缺少参数'item'的参数”。我使用 firebase Firestore 来存储我的文章数据,这里称为“项目”。当我使用 "(item.[...] ?? "") 输入我的文本时,我遇到了问题。我不知道为什么。
// This is my code where I don't have a problem
import SwiftUI
import Firebase
import Ballcap
struct SwiftUIView: View {
@ObjectBinding var dataSource: ItemDatabase = ItemDatabase()
var item: Item
var body: some View {
VStack {
Text(item.categorie ?? "")
.font(.title)
.fontWeight(.bold)
.foregroundColor(.green)
List {
ForEach(self.dataSource.items.identified(by: \.id)) { item in
MediumArticleItem(item: item.data!)
}
}
}
}
struct MediumArticleItem : View {
var item: Item
var body: some View {
HStack {
Image("test")
.resizable()
.renderingMode(.original)
.aspectRatio(contentMode: .fill)
.frame(width: 120, height: 100)
.cornerRadius(5)
.shadow(radius: 3)
Spacer()
VStack(alignment: .leading, spacing: 5.0) {
Text(item.title ?? "")
.font(.headline)
.foregroundColor(.primary)
Text(item.body ?? "")
.font(.subheadline)
.foregroundColor(.secondary)
.multilineTextAlignment(.leading)
.lineLimit(4)
}
.frame(width: 200, height: 100)
Spacer()
}
.frame(width: 330)
}
}
}
// This is the end of my View File and where I have a problem:
#if DEBUG
struct SwiftUIView_Previews: PreviewProvider {
static var previews: some View {
if FirebaseApp.app() != nil {
FirebaseApp.configure()
}
return SwiftUIView()
// the problem is here...
}
}
#endif
// ...and it proposed me to fix it buy this :
#if DEBUG
struct SwiftUIView_Previews: PreviewProvider {
static var previews: some View {
if FirebaseApp.app() != nil {
FirebaseApp.configure()
}
return SwiftUIView(item: Item) // <- but now I have a new error and it's wrote : "Cannot convert value // of type 'Item.Type' to expected argument type 'Item' "
}
}
#endif
// I have two models files for the connection to Firestore :
// This one is my Item.swift
import SwiftUI
import Ballcap
import Firebase
struct Item: Modelable, Codable, Hashable {
var title: String?
var body: String?
var body2: String?
var body3: String?
var categorie: String?
var author: String?
}
// And this one is my ItemDataSource file :
import Ballcap
import Firebase
import SwiftUI
import Combine
class ItemDatabase: BindableObject {
var willChange = PassthroughSubject<Void, Never>()
var _dataSource: DataSource<Document<Item>>?
var items: [Document<Item>] = [] {
willSet { self.willChange.send() }
}
init() {
_dataSource = DataSource<Document<Item>>.Query(Document<Item>.collectionReference).dataSource()
.onCompleted({ [weak self] (_, items) in
self?.items = items
}).listen()
}
}
我不想再有错误,我想知道为什么会出现这个错误。
【问题讨论】:
-
我认为有一个很好的答案,但你能澄清一下你想用这行代码
return SwiftUIView(item: Item)做什么吗?你希望它做什么?