Swift报错「Cannot find 'validate' in scope」问题排查求助
解决「Cannot find 'validate' in scope」错误
错误原因很直接:你在register()方法里调用的是validate(),但实际定义的校验方法拼写错误,写成了valdiate()(字母顺序出错,把validate拼成了valdiate)。
只需要把校验方法的名称修正为正确的validate()即可,修正后的完整代码如下:
import FirebaseFirestore import FirebaseAuth import Foundation class RegisterViewViewModel: ObservableObject{ @Published var name = "" @Published var email = "" @Published var password = "" init(){} func register(){ guard validate() else{ return } Auth.auth().createUser(withEmail: email, password: password) { [weak self] result, error in guard let userId = result?.user.uid else{ return } self?.insertUserRecord(id: userId) } } private func insertUserRecord(id: String){ let newUser = User(id: id, name: name, email: email, joined: Date().timeIntervalSince1970) let db = Firestore.firestore() db.collection("users") .document(id) .setData(newUser.asDictionary()) } // 修正方法名:valdiate → validate private func validate() -> Bool{ guard !name.trimmingCharacters(in: .whitespaces).isEmpty, !email.trimmingCharacters(in: .whitespaces).isEmpty, !password.trimmingCharacters(in: .whitespaces).isEmpty else{ return false } guard email.contains("@")&&email.contains(".")else{ return false } guard password.count>=6 else { return false } return true } }
建议开启Xcode的拼写检查功能(Editor > Spelling and Grammar > Check Spelling While Typing),能提前避免这类低级拼写错误。
内容的提问来源于stack exchange,提问作者Pop Gabriel-Razvan
相关产品推荐
相关产品推荐

