ios - (Swift) Storing and retrieving Array to NSUserDefaults -


i trying store array nsuserdefaults , retrieve array when needed populate uitableview.

currently using:

//store data nsuserdefaults.standarduserdefaults().setobject(myarray, forkey: "\(identity.text!)lista")                     nsuserdefaults.standarduserdefaults().synchronize()  //retrieve data let tabledata = nsuserdefaults.standarduserdefaults().stringforkey("\(identity.text!)lista") myarray = [tabledata!] tableview.reloaddata() 

but

fatal error: unexpectedly found nil while unwrapping optional value

when trying load data. not sure if issue in storage or retrieval. has been through before?

from code see storing array

// code nsuserdefaults.standarduserdefaults().setobject(myarray, forkey: "\(identity.text!)lista") 

and retrieving string

//your code let tabledata = nsuserdefaults.standarduserdefaults().stringforkey("\(identity.text!)lista") 

there type mismatch, store 1 type , retrieve type.

while retrieving either use arrayforkey() or objectforkey() see code below.

let tabledata = nsuserdefaults.standarduserdefaults().arrayforkey("\(identity.text!)lista")  

or

let tabledata = nsuserdefaults.standarduserdefaults().objectforkey("\(identity.text!)lista") 

if array go first one.


Comments