【问题标题】:How to convert items in optional Dictionary to individual strings如何将可选字典中的项目转换为单个字符串
【发布时间】:2019-04-04 02:49:59
【问题描述】:

我正在尝试将可选字典中的项目转换为单独的字符串,以便循环遍历它们并将它们转换为 URL。但一直无法这样做。

这是我用来从 firebase 获取图像的函数,它返回这个可选字典,它也包含在下面:

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()
        })
    }

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";
})

根据提供的答案,我做了以下操作:

    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()
                }
            }
        }
    })
}

但是在第三行我得到了

无法在当前上下文中推断闭包类型

编辑

要解决此错误,请将代码放在代码最深处的 do 块中,并且还要包含一个 catch 块。这将修复错误。

【问题讨论】:

标签: ios swift firebase firebase-realtime-database


【解决方案1】:

首先你需要检查可选的Dictionary 是否存在,然后循环每个键值对的字典。这是一种方法:

    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
            }
        }
    }

因此,一旦该过程完成,您将拥有imageURLs 变量中的图像。

【讨论】:

  • 我得到以下无法在当前上下文中推断闭包类型我将其包含在问题中
  • 您好,很抱歉回复晚了,但我认为这与解析问题无关。你能看看这个question它可能是你要找的东西
猜你喜欢
  • 2011-12-22
  • 2019-11-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-10
  • 2021-09-16
相关资源
最近更新 更多