将图像检索到 3 个图像视图(Firebase 和 Swift)

Retrieving images to 3 image views (Firebase and Swift)

从 JSON 结构中的这个节点开始:

{
  "Devices" : {
    "WDREMca1BOcaaW457Mg8ankzId32" : {
      "Device1" : {
        "Category" : "",
        "Description" : "",
        "DeviceName" : "",
        "ImageUrl" : "https://firebasestorage.googleapis.com/v0/b/jabeerah-c27c3.appspot.com/o/Devices_Images%2F5F4E0AC2-8279-4CC4-AE99-E797655D4331.png?alt=media&token=5823ed16-e8ff-4b85-8a64-bfb00b965785"
      },
      "Device2" : {
        "Category" : "",
        "Description" : "",
        "DeviceName" : "",
        "ImageUrl" : "https://firebasestorage.googleapis.com/v0/b/jabeerah-c27c3.appspot.com/o/Devices_Images%2FF9E2EF8D-44B3-4BE6-9020-81D190A45AA3.png?alt=media&token=af17e9a5-53f4-4054-9496-fb86d1508a6f"
      }
     },
       "a4gbFozO4gTZE6QT60b6qxQDriw2" : {
  "Device1" : {
    "Category" : "أخرى",
    "Description" : "mmm",
    "DeviceName" : "mmm",
    "ImageUrl" : "https://firebasestorage.googleapis.com/v0/b/jabeerah-c27c3.appspot.com/o/Devices_Images%2FCF13F154-2B5F-4926-A979-2E187BD0E504.png?alt=media&token=bf1b876f-9f9a-4e78-9e8d-98d017fd0404"
  }
}

我正在尝试将用户的照片检索到 his/her 个人资料。 配置文件视图有 3 个图像视图(因为每个用户只能拥有 3 个或更少的设备)。

在 viewDidLoad 函数中我做了这个:

   if ref.child("Devices").child(FIRAuth.auth()!.currentUser!.uid).child("Device1") != nil  {

ref.child("Devices").child(FIRAuth.auth()!.currentUser!.uid).child("Device1").observe(.value , with: {snapshot in
    let ImageUrl = (snapshot.value! as AnyObject).object(forKey: "ImageUrl") as? String
        self.ImageViewOne.downloadedFrom(link: ImageUrl!)

    })

} else {
    //
    }
    if ref.child("Devices").child(FIRAuth.auth()!.currentUser!.uid).child("Device2") != nil {

        ref.child("Devices").child(FIRAuth.auth()!.currentUser!.uid).child("Device2").observe(.value , with: {snapshot in
            let ImageUrl = (snapshot.value! as AnyObject).object(forKey: "ImageUrl") as? String
            self.ImageViewTwo.downloadedFrom(link: ImageUrl!)
        })

    } else {
        //
    }
    if ref.child("Devices").child(FIRAuth.auth()!.currentUser!.uid).child("Device3") != nil  {

        ref.child("Devices").child(FIRAuth.auth()!.currentUser!.uid).child("Device3").observe(.value , with: {snapshot in
            let ImageUrl = (snapshot.value! as AnyObject).object(forKey: "ImageUrl") as? String
            self.ImageViewThree.downloadedFrom(link: ImageUrl!)
        })

    } else {
        //
    }

但这实际上在

中给了我一个信号 SIGABRT 错误
let ImageUrl = (snapshot.value! as AnyObject).object(forKey: "ImageUrl") as? String

不知道为什么会这样!

对于 downloadedFrom 函数,I'm using the extension from this answer

尝试替换:-

 let ImageUrl = (snapshot.value! as AnyObject).object(forKey: "ImageUrl") as? String

至 -

 if let deviceDict = snapshot.value! as? [String : AnyObject]{
     let imageUrl = deviceDict["ImageUrl"] as! String
     self.ImageViewThree.downloadedFrom(link: ImageUrl!) 
     }