Swift MessageKit - 无法转换“_?”类型的值到预期的参数类型 'URL?'
Swift MessageKit - Cannot Convert value of type '_?' to expected argument type 'URL?'
我刚刚开始使用 MessageKit,并在 swift 中将我的代码更新到 4.2,并且一直在解决问题。然而,我正在使用 Firebase 聊天教程并在示例代码中遇到问题,这些问题引发了示例项目中不可见的错误。
无法转换“_?”类型的值到预期的参数类型 'URL?'
completion(meta?.downloadURL())
假设你的问题大概是下面的
storage.child(channelID).child(imageName).putData(data, metadata: metadata) { meta, error in
completion(meta?.downloadURL())
}
回答,swift4
storage.child(channelID).child(imageName).putData(data, metadata: metadata) { metaN, error in
// then we check if the metadata and path exist
// if the error was nil, we expect the metadata and path to exist
// therefore if not, we return an error
guard let metadata = metaN, let path = metadata.path else {
completion(nil)
return
}
// now we get the download url using the path
// and the basic reference object (without child paths)
self.getDownloadURL(from: path, completion: completion)
}
private func getDownloadURL(from path: String, completion:@escaping (URL?) -> Void) {
let firebaseStorageUrl = "gs://yourApp-Firebase-Storage.appspot.com"
let storageReference = Storage.storage().reference(forURL: firebaseStorageUrl)
storageReference.child(path).downloadURL { (url, error) in
completion(url)
}
}
确保您已在 Firebase 中启用存储,如果失败则检查控制台错误
我刚刚开始使用 MessageKit,并在 swift 中将我的代码更新到 4.2,并且一直在解决问题。然而,我正在使用 Firebase 聊天教程并在示例代码中遇到问题,这些问题引发了示例项目中不可见的错误。
无法转换“_?”类型的值到预期的参数类型 'URL?'
completion(meta?.downloadURL())
假设你的问题大概是下面的
storage.child(channelID).child(imageName).putData(data, metadata: metadata) { meta, error in
completion(meta?.downloadURL())
}
回答,swift4
storage.child(channelID).child(imageName).putData(data, metadata: metadata) { metaN, error in
// then we check if the metadata and path exist
// if the error was nil, we expect the metadata and path to exist
// therefore if not, we return an error
guard let metadata = metaN, let path = metadata.path else {
completion(nil)
return
}
// now we get the download url using the path
// and the basic reference object (without child paths)
self.getDownloadURL(from: path, completion: completion)
}
private func getDownloadURL(from path: String, completion:@escaping (URL?) -> Void) {
let firebaseStorageUrl = "gs://yourApp-Firebase-Storage.appspot.com"
let storageReference = Storage.storage().reference(forURL: firebaseStorageUrl)
storageReference.child(path).downloadURL { (url, error) in
completion(url)
}
}
确保您已在 Firebase 中启用存储,如果失败则检查控制台错误