简体   繁体   中英

Can any one help me to solve this error using Swift

would you please help me to solve this error .I'am trying to download an Image From Firebase Database, this is my code and I put a snapshot for the error . Thanks This is a snapshot for the error in Xcode

  import UIKit
  import FirebaseDatabase

  class ViewController: UIViewController , UITableViewDataSource , UITableViewDelegate {

   @IBOutlet weak var tableView: UITableView!

   var ref:FIRDatabaseReference?
   var Handle:FIRDatabaseHandle?
   var myClass = [Post]() 

   override func viewDidLoad() {
    super.viewDidLoad()

    tableView.delegate = self
    tableView.dataSource = self

    ref=FIRDatabase.database().reference()

    Handle = ref?.child("Posts").observe(.childAdded, with: { (snapshot) in
          let post = snapshot.valueInExportFormat()

          for url in post! as! [Post] {        // Error Here
             self.myClass.append(url)
            self.tableView.reloadData()
          }

          override func didReceiveMemoryWarning() {
          super.didReceiveMemoryWarning()
          // Dispose of any resources that can be recreated.
    }

   public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
          return myClass.count
     }

   public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        if  let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)as? TableViewCell{

       cell.MyImage.alpha = 0
       cell.textLabel?.text = PostData[indexPath.row]

       DispatchQueue.main.async(execute: {

       let imgurl = URL(string : self.myClass [(indexPath as       NSIndexPath).row].url)
       let imgdata = NSData(contentsOf: imgurl!)
       cell.MyImage.image = UIImage(data: imgdata as! Data)

       UIView.animate(withDuration: 0.5, animations: {
            cell.MyImage.alpha = 1
       })
   })

    return cell

    } else {

        let cell = TableViewCell()
        DispatchQueue.main.async(execute: {

            let imgurl = URL(string : self.myClass [(indexPath as NSIndexPath).row].url)
            let imgdata = NSData(contentsOf: imgurl!)
            cell.MyImage.image = UIImage(data: imgdata as! Data)
        })
        return cell
    }

 }





     }


    })


}

Maybe you want:

for url in post! {
    var wrappedPost = Post()
    wrappedPost.url = url
    ... use wrappedPost for whatever you need a Post object for
}

Sometimes simple is the way to go.

assume you have a Firebase structure

Planets
  planet_4
    some_text = "My post about Mars"
    image_url = "images/mars.jpg"
  planet_2
    some_text = "My post about Venus"
    image_url = "images/venus.jpg"

and suppose we want to load each text and image and display in a tableview. We can do it one of two ways, one at a time with .childAdded or all at once with .value. In this example, we'll walk through them one at a time.

let planetsRef = myRootRef.child("Planets")

planetsRef.observe(.childAdded, with: { snapshot in
     let dict = snapshot.value as! [String: AnyObject]

     let text = dict["text"]
     let imageUrl = dict["image_url"]

     // Create a reference to the file you want to download
     let planetRef = storageRef.child(imageUrl) //storageRef is defined elsewhere

     // Download in memory with a maximum allowed size
     //   of 1MB (1 * 1024 * 1024 bytes)
     planetRef.dataWithMaxSize(1 * 1024 * 1024) { (data, error) -> Void in
         if (error != nil) {
             // Got an error so handle it
         } else {
             // Data for "images/some planet.jpg" is returned
             // let planetImage: UIImage! = UIImage(data: data!)
             // then add the text and the image to your dataSource array
             // and reload your tableview.
         }
     })
})

This is not tested but will provide the general idea

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM