首页 > 解决方案 > 在范围内找不到“ContentView”

问题描述

我正在尝试使用 firebase 为我的应用程序创建一个登录页面,但它显示“在范围内找不到 'ContentView'”,我不知道为什么

这是我的 RecipyApp.swift

import SwiftUI
import Firebase

@main

struct RecipyApp: App{
    
    @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
    
    var body: some Scene{
        let viewModel = AppViewModel
        WindowGroup{
            ContentView()
                .environmentObject(viewModel)
                
        }
        
    }
    
}

class AppDelegate: NSObject, UIApplicationDelegate{
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
        
        FirebaseApp.configure()
        
        return true
        
    }
}

这是我的 ContentView.swift

import SwiftUI
import FirebaseAuth

class AppViewModel: ObservableObject{
    
    let auth = Auth.auth()
    @Published var signedIn = false
    
    var isSignedIn: Bool {
        
        return auth.currentUser != nil
    }
    
    
    func singIn(email: String, password: String){
        auth.signIn(withEmail: email,
                    password: password) { [weak self] result, error in
            guard result != nil, error == nil else{
                return
            }
            DispatchQueue.main.async {
            self?.signedIn = true
        }
        
    }
    
    func singUp(email: String, password: String){
        auth.createUser(withEmail: email, password: password) { [weak self] result, error in
            guard result != nil, error == nil else{
                return
            
        }
            DispatchQueue.main.async {
            self?.signedIn = true
        }
            
    }
}

struct ContentView: View {

    @EnvironmentObject var viewModel: AppViewModel
    
    var body: some View {
        NavigationView{
            if viewModel.signedIn{
                Text("Signed In")
                
            }
            else{
                SignInView()
            }

    }
        .onAppear {
            viewModel.signedIn = viewModel.isSignedIn
        }
    }
        
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
}


struct SignInView: View {
    
    @State var email = ""
    @State var password = ""
    
    @EnvironmentObject var viewModel: AppViewModel
    
    var body: some View {
       
        VStack{
            TextField("Email Address", text: $email)
                .padding()
                .background(Color(.secondarySystemBackground))
            
            SecureField("Password", text: $password)
                .padding()
                .background(Color(.secondarySystemBackground))
            
            Button(action: {
                guard !email.isEmpty, !password.isEmpty else{
                    return
                }
                viewModel.singIn(email: email, password: password)
                
            }, label: {
                Text("Sign In")
                    .frame(width: 200, height: 50)
                    .background(Color.blue)
                    .foregroundColor(.white)
                    .cornerRadius(8)
            })
        }
        .padding()
        Spacer()
            
    }
    }
        
}

struct SignUpView: View {
    
    @State var email = ""
    @State var password = ""
    
    @EnvironmentObject var viewModel: AppViewModel
    
    var body: some View {
       
        VStack{
            TextField("Email Address", text: $email)
                .padding()
                .background(Color(.secondarySystemBackground))
            
            SecureField("Password", text: $password)
                .padding()
                .background(Color(.secondarySystemBackground))
            
            Button(action: {
                guard !email.isEmpty, !password.isEmpty else{
                    return
                }
                $viewModel.singUp(email: email, password: password)
                
            }, label: {
                Text("Create Account")
                    .frame(width: 200, height: 50)
                    .background(Color.blue)
                    .foregroundColor(.white)
                    .cornerRadius(8)
            })
        }
        .padding()
        Spacer()
            
    }
    }

为什么我会收到此错误,因为我有一个 ContentView 结构?

请任何帮助将不胜感激,因为它目前已停止开发

标签: iosswiftfirebaseswiftui

解决方案


在你的 RecipyApp 你应该有:

let viewModel = AppViewModel()  // <--- with the ()

而你在 singIn 和 singUp 之后缺少 }。

在 ContentView_Previews 和 SignInView 之后你有太多 }

换句话说,检查你的括号。


推荐阅读