Swift MessageKit - Cannot Convert value of type '_?' to expected argument type 'URL?'

747 views Asked by At

I have just begun using MessageKit and have updated my code to 4.2 in swift and have been solving the problems. Yet, I am using the Firebase chat tutorial and have come across problems in the sample code that are throwing errors that aren't visible in the sample project.

Cannot convert value of type '_?' to expected argument type 'URL?'

        completion(meta?.downloadURL())
1

There are 1 answers

0
iluvatar_GR On BEST ANSWER

Assuming that your problem is probably the following

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

Answer, swift 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)
    }
}

Be sure that you have enabled Storage in Firebase and check console errors if fails