SwiftUI接入Firebase开发登录页报Cannot find 'ContentView' in scope错误
错误原因及修复方案
你遇到的报错本质是ContentView.swift文件存在多处语法错误、括号不匹配,导致整个文件编译失败,Xcode无法识别到其中定义的ContentView结构体,并非你没有定义该结构体。
具体需要修改的问题点:
RecipyApp.swift中初始化AppViewModel实例时缺少括号:
原代码:let viewModel = AppViewModel
修改为:let viewModel = AppViewModel()
前者是取类型本身,后者才是创建类实例,你之前的写法不符合Swift语法要求。
ContentView.swift中AppViewModel类的signIn、signUp方法多处缺少闭合大括号,整个类的语法结构不完整。
ContentView.swift中NavigationView缺少闭合大括号,结构体结构不完整。
SignUpView中调用singUp方法时多了不必要的$符号:
原代码:$viewModel.singUp(email: email, password: password)
修改为:viewModel.singUp(email: email, password: password)$只用于生成Binding类型绑定,调用普通方法不需要加。
- 整个ContentView.swift文件存在多处多余的闭合大括号、方法括号未对齐的问题,语法结构完全混乱,导致编译器无法正常解析文件内容。
修正后的核心代码片段参考:
修正后的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) }) Spacer() } .padding() } } 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) }) Spacer() } .padding() } }
所有语法错误修正后重新编译即可正常识别ContentView。
内容的提问来源于stack exchange,提问作者Some User 444
相关产品推荐
相关产品推荐

