ios - 在范围内找不到“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 结构?
请任何帮助将不胜感激,因为它目前已停止开发
解决方案
在你的 RecipyApp 你应该有:
let viewModel = AppViewModel() // <--- with the ()
而你在 singIn 和 singUp 之后缺少 }。
在 ContentView_Previews 和 SignInView 之后你有太多 }
换句话说,检查你的括号。
推荐阅读
- css - 如何使 Material 工具栏在滚动时不透明并在开始时透明?
- javascript - Recharts 关闭特定行的工具提示
- activemq - Apache TomEE 外部 ActiveMQ 资源未在分布式事务中回滚
- ios - SwiftUI 中的交叉淡入淡出图像似乎不起作用
- python - 使用 Pyspark 将 DataFrame 写入 Kafka 主题
- python - Django Annotated Query 以计算反向关系中使用的所有实体
- reactjs - 使用 useState 和 || 响应不正确的更新 操作员
- python - 如何在python中用0填充分钟时间序列数据?
- c++ - 为什么这个函数标记为noexcept,但可以抛出?
- c++ - 如何从基类向量中获取派生类的名称