Ios 错误的解决方案是什么;类型[User]'的值;无法转换为所需的参数类型';用户;?
在我的名为service的页面中,xcode指向用户并给出一个错误。但它不起作用。你认为我应该换什么? 我的用户已经是可选的。我认为这是一个索引问题,但我不知道如何解决。如果你能帮忙,我将不胜感激。你认为问题在哪里 斯威夫特Ios 错误的解决方案是什么;类型[User]'的值;无法转换为所需的参数类型';用户;?,ios,swift,iphone,xcode,Ios,Swift,Iphone,Xcode,在我的名为service的页面中,xcode指向用户并给出一个错误。但它不起作用。你认为我应该换什么? 我的用户已经是可选的。我认为这是一个索引问题,但我不知道如何解决。如果你能帮忙,我将不胜感激。你认为问题在哪里 斯威夫特 import Firebase struct Message { let text: String let toId: String let fromId: String var timestamp: Timestamp! var
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
}
服务,斯威夫特
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)
}
}
}
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
对象调用它:
static func fetchUser(widhtUid:String,completion:@escaping([User])->Void)
完成闭包的参数应该是
用户
,而不是用户数组-[User]
哪一行生成错误?True。。。我没有发现相反的情况,所以可能是一个问题输入错误:)是的,我找到了我的错误,谢谢用户不是用户字符串,从阵列中删除时修复了。你说是真的,它修复了这个错误。谢谢