简体   繁体   English

谁能帮助我使用Swift解决此错误

[英]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 . 您能帮助我解决此错误吗?我正尝试从Firebase数据库下载图像,这是我的代码,并且为该错误放置了快照。 Thanks This is a snapshot for the error in Xcode 谢谢这是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 假设您具有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"

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. 我们可以通过以下两种方式之一来完成此操作,一次使用.childAdded一次,或者一次使用.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 这未经测试,但可以提供总体思路

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 谁能告诉我为什么我在执行此代码时遇到错误的身份验证错误(Swift)? - Can any one tell me why i am getting Bad authentication error while executing this code(Swift)? 使用Xib和Swift的iOS本地化:有人可以帮助我使用XIB(不是Storyboard)和Swift制作多语言应用程序吗? - iOS Localization using Xib & Swift: Can someone help me to make an multilingual app using XIB (not Storyboard) and Swift? 您能帮我理解这种Swift方法吗? - Can you help me understand this Swift method? 有人可以帮助我在Swift“ Apple Mach-O Linker Error”中修复此错误吗? - Can someone help me fix this error in Swift "Apple Mach-O Linker Error? 我面临一个小问题,你们能帮帮我吗? 任何建议表示赞赏 - I'm facing with one little issue can you help me guys? any advice appreciated 有没有人可以帮助我完成我的 Swift 项目? 我有一个 Firebase 错误 - Is there anyone who can help me with my Swift Project? Ive got an Firebase error 请帮我解决m1 iOS模拟器错误 - Please help me solve the m1 iOS simulator error 谁能帮我解决在 Xcode 上使用 swift 从 Firebase 检索数据的问题? “源文件中的编辑器占位符” - Can anyone help me with this issue of retrieving data from Firebase using swift on Xcode? “Editor placeholder in source file” 在设备上运行模拟器时出现链接器错误,有人可以帮我解决这个问题吗? - Linker error while run on simulator works on device can some one help me resolve this? (迅速)有人可以帮助我对这些按钮进行大量更改吗? - (swift) Anyone can help me to make bulk change to those buttons?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM