crispy2k12
crispy2k12

Reputation: 355

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

Upvotes: 2

Views: 1460

Answers (2)

Yannick
Yannick

Reputation: 3278

In your DetailViewController add a variable called items.

class DetailViewController : UIViewController {
    var items : NSDictionary?
}

Then in your prepare for segue set the value.

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if let destView : DetailViewController = segue.destinationViewController as? DetailViewController {
        destView.items = self.items
    }
}

Upvotes: 1

Yury
Yury

Reputation: 6114

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
    }
}

Upvotes: 1

Related Questions