如何解决SwiftUI / MVVM-登录功能在模型中崩溃
这是我第一次尝试将MVVM与SwiftUI和CoreData结合使用。我正在尝试实现登录屏幕,但是当应用程序在模型中达到获取的结果时,该应用程序将崩溃。无信息。只是崩溃...
型号:
struct Model {
@FetchRequest(
entity: User.entity(),sortDescriptors: [],) var users: FetchedResults<User>
func checkLogin(username: String,password: String) {
for user in users { //Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP,subcode=0x0)
if username == user.userEmail && password == user.password {
return true
}
}
return false
}
}
查看模型:
class ViewModel {
private var model = Model()
func checkLogin(username: String,password: String) -> Bool {
model.checklogin(username: username,password: password)
}
}
最后,视图:
struct LoginView: View {
var viewModel = ViewModel()
//Login form
Button(action: {
if self.viewModel.checkLogin(username: self.email,password: self.password) {
//allow login
}
}) {
Text("login")
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。