swift - Array of structs: UserDefaults, how to use? -


i've check of topics:

how save array of custom struct nsuserdefault swift?

how save struct nsuserdefaults in swift 2.0

struct array userdefaults

i have struct containing strings , other struct: mysection.

struct mysection {   var name: string = ""   var values: [myrow] = [] } 

and there myrow store in mysection.values

struct myrow {   var value: string = ""   var quantity: string = ""   var quantitytype: string = ""   var done: string = "" } 

two arrays use it

var arraysection: [mysection] = [] var arrayrow: [myrow] = [] 

and in application, add dynamically values in arrays.

there delegate method datas second viewcontroller

func returninfos(newitem: [myrow], sectionpick: string) {     arrayrow.append(myrow())     arrayrow[arrayrow.count - 1] = newitem[0]     managesection(item: sectionpick)     listtableview.reloaddata() } 

and there managesection function.

func managesection(item: string) {     var = 0     _ in arraysection {         if arraysection[i].name == item {             arraysection.insert(mysection(), at: + 1)             arraysection[i + 1].values = [arrayrow[arrayrow.count - 1]]             return         }         += 1     }     arraysection.append(mysection())     arraysection[arraysection.count - 1].name = item     arraysection[arraysection.count - 1].values = [arrayrow[arrayrow.count - 1]] } 

my need store datas of 2 arrays in userdefaults (or coredata maybe??) , use these datas when user going application.

i don't know how it, i've try methods 3 topics i'm not doing job.

how can it?

thanks guys!

since both types contain property list compliant types suitable solution add code convert each type property list compliant object , vice versa.

struct mysection {     var name: string     var values = [myrow]()      init(name : string, values : [myrow] = []) {         self.name = name         self.values = values     }      init(propertylist: [string: any]) {         self.name = propertylist["name"] as! string         self.values = (propertylist["values"] as! [[string:string]]).map{ myrow(propertylist: $0) }     }      var propertylistrepresentation : [string: any] {         return ["name" : name, "values" : values.map { $0.propertylistrepresentation }]     } }  struct myrow {     var value: string     var quantity: string     var quantitytype: string     var done: string      init(value : string, quantity: string, quantitytype: string, done: string) {         self.value = value         self.quantity = quantity         self.quantitytype = quantitytype         self.done = done     }      init(propertylist: [string:string]) {         self.value = propertylist["value"]!         self.quantity = propertylist["quantity"]!         self.quantitytype = propertylist["quantitytype"]!         self.done = propertylist["done"]!     }      var propertylistrepresentation : [string: any] {         return ["value" : value, "quantity" : quantity,  "quantitytype" : quantitytype, "done" : done ]     } } 

after creating few objects

let row1 = myrow(value: "foo", quantity: "10", quantitytype: "foo", done: "yes") let row2 = myrow(value: "bar", quantity: "10", quantitytype: "bar", done: "no")  let section = mysection(name: "baz", values: [row1, row2]) 

call propertylistrepresentation dictionary ([string:any]) can saved user defaults.

let propertylist = section.propertylistrepresentation 

recreation of section quite easy, too

let newsection = mysection(propertylist: propertylist) 

edit

use propertylist initializer only if data userdefaults in other cases use other initializer.

for example replace

@ibaction func addbuttonpressed(_ sender: any) {     newproducts.append(myrow(propertylist: ["":""]))     newproducts[newproducts.count - 1].value = nametextfield.text!     newproducts[newproducts.count - 1].quantity = quantitytextfield.text!     newproducts[newproducts.count - 1].quantitytype = type     newproducts[newproducts.count - 1].done = "no"     delegate?.returninfos(newitem: newproducts, sectionpick: typepick)     navigationcontroller?.popviewcontroller(animated: true) } 

with

@ibaction func addbuttonpressed(_ sender: any) {      let row = myrow(value: nametextfield.text!,                     quantity: quantitytextfield.text!,                     quantitytype: type,                     done: "no")     newproducts.append(row)     delegate?.returninfos(newitem: newproducts, sectionpick: typepick)     navigationcontroller?.popviewcontroller(animated: true) } 

and replace

func returninfos(newitem: [myrow], sectionpick: string) {     arrayrow.append(myrow(propertylist: ["":""]))     arrayrow[arrayrow.count - 1] = newitem[0]     managesection(item: sectionpick)     listtableview.reloaddata() } 

with

func returninfos(newitem: [myrow], sectionpick: string) {     arrayrow.append(newitem[0])     managesection(item: sectionpick)     listtableview.reloaddata() } 

basically first create object, then append array. other way round cumbersome.


Comments

Popular posts from this blog

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -