任何人都可以帮助我使用Swift解决这个错误

时间:2016-12-13 15:12:48

标签: swift firebase swift3 firebase-realtime-database

请您帮我解决此错误。我正在尝试从Firebase数据库下载图片,这是我的代码,我为错误提供了快照。感谢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
    }

 }





     }


    })


}

2 个答案:

答案 0 :(得分:0)

也许你想要:

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

答案 1 :(得分:0)

有时简单就是要走的路。

假设您有Firebase结构

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"

并假设我们要加载每个文本和图像并在tableview中显示。我们可以采用两种方式之一,一次一个使用.childAdded,或者一次使用.value。在这个例子中,我们将逐个浏览它们。

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

这未经过测试,但会提供一般性的想法