You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 19:07:08