I have created the model class and with some static images and text I have populated the UICollectionView, but when the user touches the cell it shows an error in view controller 2 when I print or show it on a label. Below is the code.
Any suggestion?!
This is the model class
import Foundation
class Pokemon {
private var _name: String!
private var _pokedexId: Int!
// Setter And Getter
var name : String {
return _name
}
var pokedexId: Int {
return _pokedexId
}
// Initializer to initialize the data
init(name : String, pokedexId: Int) {
self._name = name
self._pokedexId = pokedexId
}
}
This is segue func in viewcontroller 1
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "pokeSegue" {
if let detailVC = segue.destinationViewController as? ViewController2 {
if let poke = sender as? Pokemon {
detailVC.pokemon = poke
}
}
}
UICollectionView delegate
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
let poke: Pokemon!
if inSearchMode {
poke = filteredPokemon[indexPath.row]
} else {
poke = pokemon[indexPath.row]
}
print(poke.name)
performSegueWithIdentifier("pokeSegue", sender: self)
}
In viewController2
import UIKit
class ViewController2: UIViewController {
var pokemon: Pokemon!
var receviceingString : String!
@IBOutlet weak var label: UILabel!
override func viewDidLoad() {
print(pokemon.name) //unexpectedly found nil while unwrapping an Optional value
}
}