简体   繁体   English

错误:无法将“CueCardModel”类型的值转换为闭包结果类型“Void”SwiftUI

[英]Error: Cannot Convert Value of Type “CueCardModel” to Closure Result Type “Void” SwiftUI

I am trying to make a Cue Card App.我正在尝试制作提示卡应用程序。 I have my DataDownloader class put my Firebase Database into an array, which the array passes to my CueCardViewModel, that returns a term and answer from a specific index to my CueCardView.我有我的 DataDownloader class 将我的 Firebase 数据库放入一个数组中,该数组传递给我的 CueCardViewModel,它从特定索引返回一个术语和答案到我的 CueCardView。

Confusing...so basically, the CueCardView should be able to call on the CueCardViewModel to get a term and answer at a specific index from the DataDownloader class.令人困惑......所以基本上,CueCardView 应该能够调用 CueCardViewModel 从 DataDownloader class 获取特定索引的术语和答案。 The problem I am having is the CueCardViewModel won't return.我遇到的问题是 CueCardViewModel 不会返回。 I am new to Swift and have tried searching for a way to do this, but I am confused because most videos/forums seem to be hardcoding their database so I am having trouble.我是 Swift 的新手,并试图寻找一种方法来做到这一点,但我很困惑,因为大多数视频/论坛似乎都在硬编码他们的数据库,所以我遇到了麻烦。

CueCardViewModel 错误图片

CueCardView:提示卡视图:

import SwiftUI

struct CueCardView: View
{
var viewModel = CueCardViewModel()
@State private var isShowingAnswer = false
var body: some View
{
    ZStack
    {
        RoundedRectangle(cornerRadius: 25, style: .continuous)
            .fill(Color.white)
        VStack
        {
            var a = getCue()
            Text(a)
                .onAppear
                {
                    self.viewModel.initializeCues(index: 1)
                }
        }
        .padding(20)
        .multilineTextAlignment(.center)
    }
    .onTapGesture
    {
        self.isShowingAnswer.toggle()
    }
  }
}

extension CueCardView {
func getCue() -> String{
    return "BBBBBBB"
 }
}
struct C1_Previews: PreviewProvider {
static var previews: some View {
    CueCardView().previewDevice("iPhone 12")
 }
}    

CueCardModel:提示卡型号:

import SwiftUI

struct CueCardModel : Identifiable {
  var id: String = UUID().uuidString
  var term: String
  var answer: String
}

DataDownloader:数据下载器:

import SwiftUI
import Firebase

var dbRef = Firestore.firestore()
struct DataDownloader {
  func downloadCues(completion: @escaping ([CueCardModel]) -> Void) {
    var array = [CueCardModel]()
    dbRef.collection("Cue").getDocuments(){(snap, err) in
        if let err = err {
            print(err)
        } else {
            guard let snap = snap else {return}
            for doc in snap.documents {
                let id = doc.documentID
                let term = doc.get("term") as! String
                let answer = doc.get("answer") as! String
                let cue = CueCardModel(id: id, term: term, answer: answer)
                array.append(cue)
            }
            completion(array)
        }
    }
  }
}

CueCardViewModel: CueCardViewModel:

import SwiftUI
import Firebase

class CueCardViewModel: ObservableObject {

let dataDownloader = DataDownloader()


func initializeCues(index :Int) -> [CueCardModel] {
    dataDownloader.downloadCues{array in
        return array[0]
    }
 }
}

initializeCues needs to be an @escaping closure similar to your downloadCues function. initializeCues 需要是类似于您的 downloadCues function 的 @escaping 闭包。 When you're downloading the data, it will take some time to get the info from the database and return.. therefore you can't just 'return' immediately.当您下载数据时,从数据库中获取信息并返回需要一些时间。因此您不能立即“返回”。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 更新到 Xcode12 后出现 ReactiveSwift 错误(错误消息:无法将“一次性”类型的值转换为关闭结果类型“Void”) - ReactiveSwift error after update to Xcode12( Error msg: Cannot convert value of type 'Disposable?' to closure result type 'Void') 无法将“()”类型的值转换为预期的参数类型“(() -&gt; Void)?” 在 swiftui - Cannot convert value of type '()' to expected argument type '(() -> Void)?' in swiftui 错误:无法使用 Swift + PromiseKit 将类型 &#39;() -&gt; ()&#39; 的值转换为闭包结果类型 &#39;String&#39; - error: cannot convert value of type '() -> ()' to closure result type 'String' using Swift + PromiseKit 无法转换类型&#39;(结果 <Model> )-&gt;无效&#39;到期望的参数类型&#39;(结果&lt;_&gt;)-&gt;无效&#39; - Cannot convert value of type '(Result<Model>) -> Void' to expected argument type '(Result<_>) -> Void' 无法将类型 &#39;() -&gt; ()&#39; 的值转换为预期的参数类型 &#39;((AuthDataResult?, Error?) -&gt; Void)?&#39; - Cannot convert value of type '() -> ()' to expected argument type '((AuthDataResult?, Error?) -> Void)?' 无法转换类型“NavigationLink”的值<some View, EditView> &#39; 到关闭结果类型 &#39;_&#39; - Cannot convert value of type 'NavigationLink<some View, EditView>' to closure result type '_' 尾随闭包传递给“结果”类型的参数<void, error> ' 不接受关闭。 Google 登录完成检查</void,> - Trailing closure passed to parameter of type 'Result<Void, Error>' that does not accept a closure. Google Sign In completion check Swift闭包:无法将类型&#39;(_) - &gt; Bool&#39;的值转换为预期的参数类型 - Swift closure: Cannot convert value of type '(_) -> Bool' to expected argument type 无法将类型为(()-&gt; Void)的值分配为类型为((()-&gt; Void)!&#39;&#39; - Cannot assign value of type '() -> Void' to type '(() -> Void)!' Swift 无法将类型“()”的值转换为预期的参数类型“() -> Void” - Swift Cannot convert value of type '()' to expected argument type '() -> Void'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM