Swift MessageKit-无法转换类型'_?'的值预期的参数类型“ URL?”

时间:2018-11-19 16:35:32

标签: swift messagekit

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

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

        completion(meta?.downloadURL())

1 个答案:

答案 0 :(得分:1)

假设您的问题可能是以下原因

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中启用存储,并检查控制台错误(如果失败)