0

I'm new to ios dev and i'm trying to develop my first app! :) so far so good... Now I'm trying to pass data from main controller to the item details view.

It seems i can't figure out what i'm doing wrong but i keep getting an error: "fatal error: unexpectedly found nil while unwrapping an Optional value"

now i understand that the value is not assigned but i don't understand why... here's my code:

 -------> MainVC

   struct  Ananlysys {
    var descrizione: String!
    var data: String!
    var immagine: String!
    var seganle: String!
    var oraUpload: String!
    var valuta: String!
}

var analysisList = [Ananlysys]()  

var alertsRef = FIRDatabase.database().reference().child("analisi")

override func viewDidLoad() {
    super.viewDidLoad()

    fetchDataFromDB()        
}

func fetchDataFromDB(){
    alertsRef.observe(.childAdded, with: { snapshot in

        if let dict = snapshot.value as? [String: AnyObject] {
            let descrizione = dict["descrizione"] as! String
            let data = dict["data"] as! String
            let stato = dict["stato"] as! String
            let immagine = dict["imageURL"] as! String
            let seganle = dict["segnale"] as! String
            let oraUpload = dict["oraupload"] as! String
            let valuta = dict["valuta"] as! String

            self.analysisList.insert(Ananlysys(descrizione: descrizione, 
                   data:data, immagine:immagine, seganle: seganle, oraUpload: oraUpload, 
                   valuta: valuta), at: 0)
            self.tableView.reloadData()
        }
    })
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
    if segue.identifier == "ItemDetailVC"{
        if let indexPath = self.tableView.indexPathForSelectedRow {
            let destination = segue.destination as! ItemDetailVC
            let cell = tableView.cellForRow(at: indexPath) as? ItemCell
            destination.valuta = (cell?.valutaLabel.text)!
            destination.segnale = (cell?.signalLabel.text)!
            destination.desc = (cell?.descriptionLabel.text)!
            destination.immagine = (cell?.imageThumb.image)!
        }
    }

And in My ItemDetailsVC controller (which is the destination) i've just created the IBoutlet. Here' the code:

class ItemDetailVC: OpenAlertsVC {

    @IBOutlet weak var crossLabel: UILabel!
    @IBOutlet weak var signalLbl: UILabel!
    @IBOutlet weak var imageDetail: UIImageView!
    @IBOutlet weak var analysisDesc: UILabel!

  var valuta = ""
  var segnale = ""
  var immagine: UIImage!
  var desc = ""
}

override func viewDidLoad() {
    super.viewDidLoad()
      crossLabel.text = valuta
      signalLbl.text = segnale
      analysisDesc.text = desc
      imageDetail.image = immagine
}

Probably i'm just doing some stupid error... :) but it's my first app and i really don't know how to figure this out!

If anybody could help that would be much appreciate! :)

Thank you! Cheers!

UPDATE: I think my problems is because of the image. I have an url which i retrive from firebase in this way:

   let url = analysisList[indexPath.row].immagine

and then i download async the images:

   cell.imageThumb.downloadImageFrom(link: url!, contentMode: 
    UIViewContentMode.scaleToFill)

in the segue i do this:

   destination.immagine = (cell?.imageThumb.image)!

and in my DetailVC:

 var immagine: UIImage!
 imageDetail.image = immagine

this is the screen of the error enter image description here

AL.
  • 36,815
  • 10
  • 142
  • 281
Marco
  • 1,051
  • 1
  • 17
  • 40
  • Possible duplicate of [Passing Data between View Controllers](https://stackoverflow.com/questions/5210535/passing-data-between-view-controllers) – Chris May 29 '17 at 18:58
  • Can you be a bit more specific as to what line is giving you that error? If you don't know, simply add a breakpoint in your code and step through it one line at a time until it crashes. – Jay May 29 '17 at 19:28
  • it seem that i'm having problem with the image...i have a url in firebase which i retrieve but it seem that i'm not able to pass it... and it gives me the error found nil – Marco May 29 '17 at 19:53
  • it gives me the error on this line: tableView.delegate = self – Marco May 29 '17 at 20:40
  • `viewDidLoad` is probably being called before you are setting `destination.analysisNew = label` what I would suggest doing it rather have a function that updates the label when you pass in a new `analysisNew` i.e `func setAnalysisNew(_ analysisNew: ) { crossLabel.text = analysisNew.valuta }` – sbarow May 29 '17 at 20:57

1 Answers1

0

Saw this recently passing variables between views. I was able to get them moving by first setting the value to a top level variable (when a row is selected) and then re-reference that variable during the segue prepare function. Hope this helps.

Set a top level variable:

var valuta: String!

Breakout the self.tableView.indexPathForSelectedRow section into it's own delegate. This tutorial is a great example. (http://shrikar.com/swift-ios-tutorial-uisearchbar-and-uisearchbardelegate)

Using the "didSelectRowAt indexPath" delegate, set the top level variable value.

func tableView(_ tableView: UITableView, didSelectRowAt IndexPath: IndexPath){
    let cell = tableView.cellForRow(at: indexPath)!
    valuta = cell?.valutaLabel.text
}

Sample adjustments to the prepare function:

override func prepare(for segue: UIStoryboardSegue, sender: Any?){ 
    if segue.identifier == "ItemDetailVC"{
        if let destination = 
            segue.destination as? name_of_your_next_view_controller {
                destination.passedValuta = valuta
    }
}

Make sure to put a receiving variable in the next view

var passedValuta: String!
  • Thanks guys! Solved it! it was actually all my bad... the code was working... i forgot that during a test i've changed the declaration of the ItemDetailVC and it was not a UIviewController.... I'm Dumb! :D – Marco May 30 '17 at 07:36