0

我刚刚开始使用 MessageKit 并迅速将我的代码更新到 4.2 并一直在解决问题。然而,我正在使用 Firebase 聊天教程,并且在示例代码中遇到了一些问题,这些问题引发了示例项目中不可见的错误。

无法转换类型“_?”的值 到预期的参数类型“URL?”

        completion(meta?.downloadURL())
4

1 回答 1

2

假设您的问题可能如下

storage.child(channelID).child(imageName).putData(data, metadata: metadata) { meta, error in
  completion(meta?.downloadURL())
}

回答,迅速 4

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 中启用存储并在失败时检查控制台错误

于 2018-11-23T18:21:50.417 回答