swift - Accessing/Updating Nested Dictionary through Segues -


i'm having problem. i'm making app using swift choose continent through buttons on main view controller. takes second view controller countries of continent , populations show on text view. later add country , population third view controller using text fields, add information previews text view. adding respective continents.

i have nested dictionary in first view controller. how add information third view , show on second view when data model on first view?

this data model:

var dictionary : [string: [string: int]] = [  "north america" : ["country1" : 0, "country2" : 0, "country3" : 0], "asia"          : ["country1" : 0, "country2" : 0, "country3" : 0], "south america" : ["country1" : 0, "country2" : 0, "country3" : 0], "africa"        : ["country1" : 0, "country2" : 0, "country3" : 0], "europe"        : ["country1" : 0, "country2" : 0, "country3" : 0], "oceania"       : ["country1" : 0, "country2" : 0, "country3" : 0]  ] 

i'm new swift that's why of complete confusion.

this example in swift 3

from understand, need pass , mutate dataset between views.

i'll provide simple way pass dictionary between views, you'll able pass data-set along, edit it, , changes data set traverse between views.

define protocol tells view controllers must have dictionary of type [string: [string: int]] variable.

protocol viewpassesdata : class {     var dictionary : [string: [string: int]] { set } } 

make each of view controllers handling data conform protocol.

class firstviewcontroller : uiviewcontroller, viewpassesdata { //...}  class secondviewcontroller : uiviewcontroller, viewpassesdata { //...}  class thirdviewcontroller : uiviewcontroller, viewpassesdata { //...} 

you'll need add first view controller:

var dictionary : [string: [string: int]] = [          "north america" : ["country1" : 0, "country2" : 0, "country3" : 0],         "asia"          : ["country1" : 0, "country2" : 0, "country3" : 0],         "south america" : ["country1" : 0, "country2" : 0, "country3" : 0],         "africa"        : ["country1" : 0, "country2" : 0, "country3" : 0],         "europe"        : ["country1" : 0, "country2" : 0, "country3" : 0],         "oceania"       : ["country1" : 0, "country2" : 0, "country3" : 0]      ] 

and view controllers 2 & 3

var dictionary : [string: [string: int]] = [:] 

now you've defined common interface between view controllers , you've told view controllers must adhere commonality, can paste prepare segue function each of view controllers. note we're casting our view controllers our protocol type. since each of our view controllers adhere our protocol, cast should go on fine , should able access dictionary lives in controller we're casting to.

override func prepare(for segue: uistoryboardsegue, sender: anyobject?) {           guard let vc = segue.destinationviewcontroller as? viewpassesdata        else { fatalerror("wrong vc type") }      vc.dictionary = self.dictionary } 

to test i've shown you, add print statement viewdidload in each view:

override func viewdidload() {     super.viewdidload()     print("dictionary: \(dictionary)") } 

you'll see data set being transferred between views. you'll able update data set , pass next view.


Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -