Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

unexpectedly found nil while unwrapping an Optional value - please help

jarvjarv Posts: 210Registered Users @ @
Please help, I am trying to pass data from one tableViewController to another, I get the following error: unexpectedly found nil while unwrapping an Optional value

here is my code:
import UIKit

class ViewController: UITableViewController {
    var NumberOfRows = 0
    var townsArray = [String]()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        parseJSONtowns()
    }
    
    func parseJSONtowns() {
        let url = NSURL(string: "http://www.mypubspace.com/pubsmobile/towns.php";)
        let jsonData = NSData(contentsOfURL: url!)
        let readableJSON = JSON(data: jsonData!, options: NSJSONReadingOptions.MutableContainers, error: nil)
        
       
        for (_,object):(String, JSON) in readableJSON {
            let town = object["rstown"].string as String!
            townsArray.append(town)
            NumberOfRows = townsArray.count
        }
        NSLog("\(NumberOfRows)")
        
    }
    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return NumberOfRows
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell!
        
        if townsArray.count != 0{
            
            cell.textLabel?.text = townsArray[indexPath.row]
            
        }
        
        return cell
    }
    
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
        if segue.identifier == "towntopub" {
            if let destination = segue.destinationViewController as? townPubs {
                let path = tableView.indexPathForSelectedRow
                let cell = tableView.cellForRowAtIndexPath(path!)
                destination.viaSegue = (cell?.textLabel?.text!)!
            }
        }
    }
    
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        _ = tableView.indexPathForSelectedRow!
        if let _ = tableView.cellForRowAtIndexPath(indexPath) {
            self.performSegueWithIdentifier("towntopub", sender: self)
        }
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
        
    }
    
    
}

Sign In or Register to comment.