简体   繁体   English

无法将类型“用户”的返回表达式转换为返回类型“用户?” 从 Firebase 获取 currentUser

[英]Cannot convert return expression of type 'User' to return type 'User?' for getting currentUser from Firebase

I'm new to Swift and I'm having trouble returning a User?我是 Swift 的新手,我无法返回用户? value from currentUser in Firebase for CURRENT_USER. CURRENT_USER 的 Firebase 中的 currentUser 的值。 I have my User class declaration below and it's located in a separate UserApi file.我在下面有我的用户 class 声明,它位于单独的 UserApi 文件中。 All the help is greatly appreciated!非常感谢所有帮助!

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

User declaration:用户声明:

class User {
    var email: String?
    var profileImageUrl: String?
    var username: String?
    var id: String?
    var isFollowing: Bool?
}

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

Since you have declared a User type in your project, the word User in var CURRENT_USER: User? {由于您在项目中声明了一个User类型, var CURRENT_USER: User? { User var CURRENT_USER: User? { gets resolves to your own User type, rather than FirebaseAuth.User , which is in another module. var CURRENT_USER: User? {解析为您自己的User类型,而不是另一个模块中的FirebaseAuth.User

To fix this, either:要解决此问题,请执行以下任一操作:

  • rename your own User to something such as UserInfo , or;将您自己的User重命名为诸如UserInfo之类的名称,或者;

  • Add the prefix FirebaseAuth.添加前缀FirebaseAuth. to User to differentiate:User区分:

     var currentUser: FirebaseAuth.User? { Auth.auth().currentUser }

    You can also add a typealias to give the Firebase User a different name:您还可以添加一个typealias给 Firebase User一个不同的名称:

     typealias FirebaseUser = FirebaseAuth.User

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将类型为“用户”的返回表达式转换为类型为“用户”? - Cannot convert return expression of type 'User' to return type 'User?' 无法将类型“[()]”的返回表达式转换为返回类型 - Cannot convert return expression of type '[()]' to return type 无法转换返回表达式的类型 - Cannot convert return expression of type 无法将类型“()”的返回表达式转换为返回类型“T” - Cannot convert return expression of type '()' to return type 'T' Swift闭包:无法将类型'()'的返回表达式转换为类型'LiveSearchResponse?” - Swift Closures: Cannot convert return expression of type '()' to return type 'LiveSearchResponse?' 无法将返回表达式类型转换为带闭包的返回类型? - Cannot convert return expression type to return type with closure? 无法将'Never'类型的返回表达式转换为'String'类型的返回值 - Cannot convert return expression of type 'Never' to return type 'String' 无法将类型为'child'的返回表达式转换为返回类型T. - Cannot convert return expression of type 'child' to return type T 无法将“MopubBannerAdView”类型的返回表达式转换为“UIViewController”类型? - Cannot convert return expression of type 'MopubBannerAdView' to return type 'UIViewController?' 无法将类型 () 的返回表达式转换为返回类型 WebImage? - Cannot convert return expression of type () to return type WebImage?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM