在范围内找不到“ContentView”[关闭]

Posted

技术标签:

【中文标题】在范围内找不到“ContentView”[关闭]【英文标题】:Cannot find 'ContentView' in scope [closed] 【发布时间】:2021-07-03 08:02:53 【问题描述】:

我正在尝试使用 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 结构?

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

【问题讨论】:

您的 ContentViewContentView_Previews 结构在您的 AppViewModel 中。把它们移到外面。 它可以帮助我们在代码格式正确时为您提供帮助。照原样,看起来 ContentView 在 AppViewModel 之外,但正如@rbaldwin 所建议的那样,它实际上在引发错误的模型内部。发布问题时,请花时间格式化您的代码以提高可读性(和清晰性!)。 此外,为了可读性和清晰度,请尝试将每个 ViewClass 分隔在自己的文件中 选择你的代码并按CTRL+I重新格式化,你会看到问题。 【参考方案1】:

在您的 RecipyApp 中,您应该有:

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

你在 singIn 和 singUp 之后都缺少 。

在 ContentView_Previews 和 SignInView 之后你有一个太多的

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

【讨论】:

以上是关于在范围内找不到“ContentView”[关闭]的主要内容,如果未能解决你的问题,请参考以下文章

在范围内找不到“LocationsData”

Xcode ViewController 显示在范围内找不到“支付”

在范围内找不到类型“ListenerRegistration”

在范围内找不到 MPRemoteCommandCenter

在范围内找不到 UIDocumentPickerViewController

在范围内找不到类型“配置”