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

jarvjarv Posts: 206Registered Users @ @
Please help, I get the following error:
fatal error: unexpectedly found nil while unwrapping an Optional value
(lldb)

import UIKit
import Alamofire
import SwiftyJSON

class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {

    @IBOutlet weak var collectionView: UICollectionView!
    var town : NSArray!
    var delegate : AppDelegate?
    override func viewDidLoad() {
        super.viewDidLoad()
        
        Alamofire.request(.GET, "http://www.mypubspace.com/json/listtowns.php";).responseJSON { (response) -> Void in
                if let value = response.result.value {
                    let json = JSON(value)
                    //If json is .Dictionary
                    for (key: _, subJson: JSON) in json {
                        self.town = JSON["town"] as! NSArray
                        print(self.town)
                    }
                }
            
        }
        
        // Do any additional setup after loading the view, typically from a nib.
    }

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

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.town.count
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! TownCollectionViewCell
        
        cell.townLabel?.text = self.town[indexPath.row] as? String
        
        return cell
    }
    
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        self.performSegueWithIdentifier("showTown", sender: self)
    }
    
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "showTown"{
            let indexPaths = self.collectionView!.indexPathsForSelectedItems()!
            let indexPath = indexPaths[0] as NSIndexPath
            
            let vc = segue.destinationViewController as! TownController
            vc.title = self.town[indexPath.row] as? String
        }
    }
    
}
Tagged:

Replies

Sign In or Register to comment.