gpt4 book ai didi

ios - 无法将类型 'UserFile' 的值转换为预期参数 'User'

转载 作者:行者123 更新时间:2023-11-28 15:25:58 25 4
gpt4 key购买 nike

我正在使用 Swift 3 和 Firebase 制作一个 iOS 应用。我不断收到错误消息“无法将‘UserFile’类型的值转换为预期参数‘User’”。这是我的代码:

import Foundation
import Firebase
import FirebaseAuth


class UserAPI {

var REF_USERS = Database.database().reference().child("users")

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

var CURRENT_USER_ID = Auth.auth().currentUser?.uid

var REF_CURRENT_USER: DatabaseReference? {
guard let currentUser = Auth.auth().currentUser else {
return nil
}
return REF_USERS.child(currentUser.uid)
}

func observeCurrentUser(completion: @escaping (User) -> Void) {
guard let currentUser = Auth.auth().currentUser else {
return
}

REF_USERS.child(currentUser.uid).observeSingleEvent(of: .value, with: { snapshot in
if let postDictionary = snapshot.value as? [String: Any] {
let user = UserFile.transformUser(postDictionary: postDictionary)
completion(user) //Cannot convert value of type 'UserFile' to expected argument 'User'
}
})
}

func observeUser(withID uid:String, completion: @escaping (User) -> Void) {
REF_USERS.child(uid).observeSingleEvent(of: .value, with: { snapshot in
if let postDictionary = snapshot.value as? [String: Any] {
let user = UserFile.transformUser(postDictionary: postDictionary)
completion(user) //Cannot convert value of type 'UserFile' to expected argument 'User'
}
})
}

}

'UserFile' 是另一个 Swift 文件,下面是它的代码:

import Foundation

class UserFile {
var email: String?
var profileImageURL: String?
var username: String?
}

extension UserFile {

static func transformUser(postDictionary: [String: Any]) -> UserFile {
let user = UserFile()

user.email = postDictionary["email"] as? String
user.profileImageURL = postDictionary["profileImageURL"] as? String
user.username = postDictionary["username"] as? String

return user
}

}

我不知道我应该做什么。有什么想法吗?

最佳答案

您的函数显示完成类型为 user: func observeCurrentUser(completion: @escaping (User) -> Void) {并且您将 UserFile 传递给它.

将完成类型更改为UserFile,像这样:(completion: @escaping (UserFile) -> Void)

关于ios - 无法将类型 'UserFile' 的值转换为预期参数 'User',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45224978/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com