错误“[User]类型的值'无法转换为预期的参数类型'User”错误的解决方案是啥?

Posted

技术标签:

【中文标题】错误“[User]类型的值\'无法转换为预期的参数类型\'User”错误的解决方案是啥?【英文标题】:What is the solution to the error "The value of type [User] 'cannot be converted to the expected argument type' User"?错误“[User]类型的值'无法转换为预期的参数类型'User”错误的解决方案是什么? 【发布时间】:2020-11-07 19:59:43 【问题描述】:

在我的名为 service 的页面中,xcode 指向用户并给出错误。但它不起作用。你觉得我应该改变什么? 我的用户已经是可选的。我认为这是一个索引问题,但我不知道如何解决,如果您能提供帮助,我将不胜感激。您认为问题在哪里

message.swift

import Firebase

struct Message 
    let text: String
    let toId: String
    let fromId: String
    var timestamp: Timestamp!
    var user: User?
    let isFromCurrentUser :Bool
    
    init(dictionary: [String: Any]) 
        self.text = dictionary["text"] as? String ?? ""
        self.toId = dictionary["toId"] as? String ?? ""
        self.fromId = dictionary["fromId"] as? String ?? ""
        self.timestamp = dictionary["timestamp"] as? Timestamp ?? Timestamp(date: Date())
        self.isFromCurrentUser = fromId == Auth.auth().currentUser?.uid
    
    
    


struct Conversation 
    let user: User
    let message : Message

Service.Swift

import Firebase
 
struct Service  
    static func  fetchUsers (completion: @escaping([User]) -> Void) 
        var users = [User] ()
       COLLECTION_USERS.getDocuments  (snapshot, error) in
            snapshot?.documents.forEach( (document) in
               
                let dictionary  = document.data()
                let user = User(dictionary: dictionary)
                users.append(user)
                completion(users)
            
            )
        
    
    
    static func fetchUser(widhtUid uid: String, completion:@escaping([User]) ->Void) 
        
COLLECTION_USERS.document(uid).getDocument  (snapshot, error) in
    guard let dictionary = snapshot?.data() else return
    let user = User(dictionary: dictionary)
    completion(user)
        
        
        
    
    
    
    static func fetchConversations (completion: @escaping([Conversation]) ->Void) 
        var conversations = [Conversation]()
        guard let uid = Auth.auth().currentUser?.uid else return
        
        let query = COLLECTION_MESSAGES.document(uid).collection("recent-messages").order(by:  "timestamp")
        query.addSnapshotListener  (snapshot, error) in
            snapshot?.documentChanges.forEach( change in
                let dictionary = change.document.data()
                let message = Message(dictionary: dictionary)
                
                self.fetchUser(widhtUid: message.toId)  user in
                    let conversation = Conversation(user:user, message: message)
                    conversations.append(conversation)
                    completion(conversations)
                
                
           
            )
        
        
    
    
    
    static func fetchMessages    (forUser user: User, completion: @escaping([Message])-> Void)  
    var messages  = [Message]()
    guard let currentUid = Auth.auth().currentUser?.uid else return
        let query = COLLECTION_MESSAGES.document(currentUid).collection(user.uid).order(by: "timestamp")
        query.addSnapshotListener(snapshot,error) in
        snapshot?.documentChanges.forEach( change in
            if change.type == .added 
                let dictionary = change.document.data ()
                
                messages.append(Message(dictionary: dictionary))
                completion(messages)
            
        
            
            
            
        )
    
    
 static func  uploadMessage(message: String, to user: User, completion: ((Error?)->Void)?) 
        guard let currentUid = Auth.auth().currentUser?.uid else return
        let data = ["text": message,
                    "fromId": currentUid,
                    "toId": user.uid,
                    "timestamp" : Timestamp(date: Date())] as [String : Any]
           COLLECTION_MESSAGES.document(currentUid).collection(user.uid).addDocument(data:data)  _ in
                COLLECTION_MESSAGES.document(user.uid).collection(currentUid).addDocument(data:data,completion:completion)
            
            COLLECTION_MESSAGES.document(currentUid).collection("recent- messages").document(user.uid).setData(data)
            
            COLLECTION_MESSAGES.document(user.uid).collection("recent- messages").document(currentUid).setData(data)
            
            
        
    

【问题讨论】:

哪一行产生了错误? 【参考方案1】:

在这个方法中:

static func fetchUser(widhtUid uid: String, completion:@escaping ([User]) -> Void)

完成闭包的参数应该是User,而不是用户数组 - [User]

【讨论】:

你说的是真的,它修正了这个错误。谢谢【参考方案2】:

Xcode 应该将您指向发生此错误的行...

总之,这里

static func fetchUser(widhtUid uid: String, completion:@escaping([User]) ->Void) 
        
COLLECTION_USERS.document(uid).getDocument  (snapshot, error) in
    guard let dictionary = snapshot?.data() else return
    let user = User(dictionary: dictionary)
    completion(user)
        
        
        
    

您的 completion:@escaping([User]) ->Void) 需要一个数组 [User] ,但您在此处仅使用一个 User 对象 completion(user) 调用它

【讨论】:

是的...虽然我没有找到相反的情况,所以也许这是一个问题错字:) 是的,我明白了,我发现了我的错误,谢谢用户不是用户字符串,从数组中删除时已修复

以上是关于错误“[User]类型的值'无法转换为预期的参数类型'User”错误的解决方案是啥?的主要内容,如果未能解决你的问题,请参考以下文章

Informix 错误:发生了语法错误。错误代码:-201

我收到一个错误:“MetaMask - RPC 错误:错误:错误:[ethjs-rpc] rpc 错误与有效负载”

错误精灵错误跟踪器错误

网页打开显示错误500是啥意思

PHP错误处理

PHP的错误处理