0

I am trying to convert items in an optional dictionary into individual strings so I can loop through them and convert them into URLs. But have been unable to do so.

Here is function which I use to fetch images from firebase which returns this optional dictionary which is also included below:

func fetchAllUsersImages() {
        print("inside func")
        self.ref.child("Posts").child(self.userID).child(self.postNum).observe(.childAdded, with: { snapshot in
            print("inside closure")
//            print(URL(string: snapshot.value as! String))
//            let postSnap = snapshot.childSnapshot(forPath: self.postNum)
//            let imageUrlSnap = postSnap.childSnapshot(forPath: "ImageUrl")
            print(snapshot.value, "value")
//            guard let allImages = imageUrlSnap.children.allObjects as? [DataSnapshot] else { return print("the code failed here")}
            guard let allImages = snapshot.value as? [DataSnapshot] else { return print("the code failed here")}
//            let snapshotVal = snapshot.value
//            let snapshotValValue = snapshotVal as! String
//            print(snapshotValValue, "snapshot as string value")
            for image in allImages {
                print(image, "image")
            }
            print(snapshot.key, "key")
            print(snapshot.value, "value")
            print(snapshot.children, "cjildren")
            print(allImages)
            print()
        })
    }

Output of snapshot.value:

Optional({
    image1 = "https://firebasestorage.googleapis.com/v0/b/base.appspot.com/o/ijzAnEdyKNbhPsQVH6a8mOa1QpN2%2Fpost1%2Fimage1?alt=media&token=c2f396fd-717d-4192-909a-db390dd23143";
    image2 = "https://firebasestorage.googleapis.com/v0/b/atabase.appspot.com/o/ijzAnEdyKNbhPsQVH6a8mOa1QpN2%2Fpost1%2Fimage2?alt=media&token=359b8527-f598-4f9a-934e-079cee21fd15";
})

Based on the answer provided I did the followoing:

    func fetchAllUsersImages() {
    print("inside func")
    self.ref.child("Posts").child(self.userID).child(self.postNum).observe(.childAdded, with: { snapshot in //error here

        var images: [URL] = []
        if let snapShotValue = snapshot.value as? [String: String] {

            for (_, value) in snapShotValue {
                if let imageURL = URL(string: value) {
                    print(imageURL, "image url here")
                    let imageAsData = try Data(contentsOf: imageURL)
                    let image = UIImage(data: imageAsData)
                    let ImageObject = Image()
                    ImageObject.image = image
                    self.arrayOfImgObj.append(ImageObject)
                    self.tableView.reloadData()
                }
            }
        }
    })
}

However on the 3rd line I get

Unable to infer closure type in the current context

Edit:

To fix this error put the code, at the deepest part of the code, in a do block amd include a catch block also. This will fix the error.

  • Does your program only output this ? – Pop Flamingo Oct 31 '18 at 01:05
  • No in terms of this class it also outputs:Description: post1 Description: ijzAnEdyKNbhPsQVH6a8mOa1QpN2 inside func inside closure Optional({ image1 = "https://firebasestorage.googleas.com/v0/b/database.appspot.com/o/ijzAnEdyKNbhPsQVH6a8mOa1QpN2%2Fpost1%2Fimage1?alt=media&token=c2f396fd-717d-4192-909a-db390dd23143"; image2 = "https://firebasestorage.googleapis.com/v0/b/database.appspot.com/o/ijzAnEdyKNbhPsQVH6a8mOa1QpN2%2Fpost1%2Fimage2?alt=media&token=359b8527-f598-4f9a-934e-079cee21fd15"; }) value the code failed here –  Oct 31 '18 at 01:08

1 Answers1

0

Well first you need to check if the optional Dictionary exists then loop the dictionary for each key-value pair. Here is a way to do it:

    var imageURLs: [URL] = []
    if let snapShotValue = snapshot.value as? [String: String] { // Cast optional dictionary to a Dictionary of String keys and String values
// Cast would fail if snapshot.value is nil or has a different Dictionary setup.
        for (key, value) in snapShotValue { // you can change key to _ since we are not using it
            if let imageURL = URL(string: value) { // Get URL value from string
                imageURLs.append(imageURL) // Add new URL to parsed URLs
            }
        }
    }

So once the process is finished you'll have the images in imageURLs variable.

Ayazmon
  • 1,360
  • 8
  • 17
  • I get the following Unable to infer closure type in the current context I included it in teh question –  Oct 31 '18 at 21:21
  • Hi sorry for a late reply but I think this is not related with a parsing issue. Can you please check out this [question](https://stackoverflow.com/questions/53092951/unable-to-infer-closure-type-in-the-current-context) it might be what you are looking for – Ayazmon Nov 01 '18 at 07:39