Contextual type 'Void' cannot be used with dictionary literal

584 views ios

This Error comes up after I relaunched my Project without any changes, never heard of it before.

func toDictionary() -> [String : Any] {

    let newPostRef = Database.database().reference().child("posts").childByAutoId()
    let newPostKey = newPostRef.key

    // 1. save image
    if let imageData = image.jpegData(compressionQuality: 0.5) {
        let storage ="images/\(newPostKey)")

        storage.putData(imageData).observe(.success, handler: { (snapshot) in
            self.downloadURL = snapshot.metadata?.downloadURL()?.absoluteString

        return [ <- The Error appears here!!!
        "text" : text,
        "imageDownloadURL" : downloadURL,
        "numberOfLikes" : numberOfLikes,
        "numberOfDislikes" : numberOfDislikes

Maybe the following lines of Code help, as I only read something that this Error occurs because of any false String or something like that...

var text: String = ""
private var image: UIImage!
var downloadURL: String?
var numberOfLikes = 0
var numberOfDislikes = 0
let ref: DatabaseReference!

init(snapshot: DataSnapshot){
    ref = snapshot.ref
    if let value = snapshot.value as? [String : Any] {
        text = value["text"] as! String
        downloadURL = value["imageDownloadURL"] as? String
        numberOfLikes = value["numberOfLikes"] as! Int
        numberOfDislikes = value["numberOfDislikes"] as! Int

answered question

1 Answer


You want to return [String: Any], but you return return (void)

You can return [String : Any]() instead of only return

Or make your return optional like

func toDictionary() -> [String : Any]?

and return nil

posted this

Have an answer?


Please login first before posting an answer.