呼叫中缺少参数'item'的参数

时间:2019-07-27 17:11:49

标签: swift firebase swiftui

我正在使用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()
    }
}

我希望没有更多的错误,我想知道为什么会有这个错误。

1 个答案:

答案 0 :(得分:0)

您的SwiftUIView的属性定义为:

var item: Item

这意味着除非您为视图创建初始化程序,否则编译器将为您合成一个初始化程序。在这种情况下,可能会发生以下情况:

init(dataSource: ItemDatabase = ItemDatabase(), item: Item) {
  self.dataSource = dataSource
  self.item = item
}

这就是为什么您必须通过将项目作为参数传递来创建视图的原因。

至于错误:

SwiftUIView(item: Item) // dataSource can be omitted, because the initilizer has a default value

Item不是值,而是类型。假设您有一个需要整数的函数,而不是这样做:myfunc(value: 3),而是这样做:myfunc(value: Int)。这就是编译器告诉您的内容,但是类型是Item,而不是Int

因此,您需要使用其初始值设定项来创建Item类型的值:

SwiftUIView(item: Item(title: "abc", ....)) // use the right initializer here