无法将类型 'User' 的返回表达式转换为返回类型 'User?'



我有这个错误:

无法将类型为"用户"的返回表达式转换为返回类型"用户?

    var CURRENT_USER: User? {
        if let currentUser = Auth.auth().currentUser {
           return currentUser
        }
        return nil
    }

User声明:

class User {
    var email: String?
    var ProfileImageUr: String?
    var username: String?
    var id: String?
}
extension User {
    static func transForUser(dict: [String: Any], key: String) -> User {
        let user = User()
        user.email = dict["email"] as? String
        user.ProfileImageUr = dict["ProfileImageUr"] as? String
        user.username =  dict["username"] as? String
        user.id = key
        return user
    }
}

这样做会返回未包装的用户。

尝试

return Auth.auth().currentUser

而不是

if let currentUser = Auth.auth().currentUser {
           return currentUser
}
return nil

有两个不同的类,都称为用户。

  • 一个是你创建的,另一个来自FirebaseAuth。

该解决方案只是将类重命名为另一个名称。

相关内容

最新更新