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

问题描述 投票:1回答:1

我有这个错误:

无法将“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
    }
}
swift
1个回答
0
投票

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

尝试

return Auth.auth().currentUser

代替

if let currentUser = Auth.auth().currentUser {
           return currentUser
}
return nil
© www.soinside.com 2019 - 2024. All rights reserved.