crispy2k12 crispy2k12 - 3 months ago 17
Swift Question

send dictionary through segue in swift

I have a dictionary declared in my first view controller and i want to pass the dictionary through the segue to the detail view controller, the only problem is that i'm not entirely sure how to achieve this?

Dictionary in first view Controller:

var items = [NSDictionary]()


Detail View Controller:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var Pr : DetailViewController = segue.destinationViewController as! DetailViewController

Answer

var items = [NSDictionary]() - this is array of elements of NSDictionary. If you wish NSDictionary - declare it without square braces:

var items = NSDictionary()

Now, in DetailViewController you should declare property, that will able hold passed dictionary. It can be exactly same named:

class DetailViewController: UIViewController {
    var items = NSDictionary()
    /* ... */
}

Now you can use prepareForSegue for passing dictionary (you should use segue identifier, setted in storyboard):

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "yourSegue" {
        let Pr : DetailViewController = segue.destinationViewController as! DetailViewController
        Pr.items = items
    }
}