arrays - How to Save complex Arrayobjects to device using NSKeyedArchiver swift -


i want save array of class (e.g let array = symptomsmodel) type device using nskeyedarchiver in swift .

i know how save array if symptomsmodel class contains variables primitive data types , don't know how save if contains array of other class property

below have explained problem of example , please go through , provide solution. have class

class symptomsmodel: nsobject, nscoding ,responsejsonobjectserializable   {             var slug:string?             var name:string?             var images:[sym_images]?             var videos:[sym_videos]?      struct keys {             static let name = "name"             static let slug = "slug"             static let images = "images"             static let videos = "videos"          }       required init(json:swiftyjson.json) {          self.slug = json["slug"].string          self.name = json["name"].string           self.images = [sym_images]()          if let imagesjson = json["images"].array {                 for(imagesjson) in imagesjson {                     if let newimages = sym_images(json: imagesjson){                         self.images?.append(newimages)                     }                 }            }            self.videos = [sym_videos]()           if let videosjsonarray = json["videos"].array {                  for(videosjson) in videosjsonarray {                     if let newvideos = sym_videos(json: videosjson){                         self.videos?.append(newvideos)                     }                 }             }        }     init(dictionary: [string : anyobject]) {      self.name = dictionary[keys.name] as? string      self.slug = dictionary[keys.slug] as? string      self.images = dictionary[keys.images] as? [sym_acc_images_objects]       self.videos = dictionary[keys.videos] as? [sym_acc_videos_objects]     } func encodewithcoder(archiver: nscoder) {     archiver.encodeobject(name, forkey: keys.name)     archiver.encodeobject(slug, forkey: keys.slug)      archiver.encodeobject(images, forkey: keys.images)     archiver.encodeobject(videos, forkey: keys.videos) } required init(coder unarchiver: nscoder) {     super.init()     name = unarchiver.decodeobjectforkey(keys.name) as? string     slug = unarchiver.decodeobjectforkey(keys.slug) as? string     self.images =  unarchiver.decodeobjectforkey(keys.slug) as? [sym_acc_images_objects]     self.videos =  unarchiver.decodeobjectforkey(keys.slug) as? [sym_acc_videos_objects]    } 

and persistancemanager class save data nskeyedarchiver as

    class persistencemanager {      class private func documentsdirectory() -> nsstring {         let paths = nssearchpathfordirectoriesindomains(.documentdirectory, .userdomainmask, true)         let documentdirectory = paths[0] string         return documentdirectory     }      class func savensarray(arraytosave: nsarray, key: string) {         print(" savensarray key : \(key)")         let file = documentsdirectory().stringbyappendingpathcomponent(key)         nskeyedarchiver.archiverootobject(arraytosave, tofile: file)      }      class func loadnsarray(path: string) -> nsarray? {         print(" loadnsarray key : \(path)")         let file = documentsdirectory().stringbyappendingpathcomponent(path)         let result = nskeyedunarchiver.unarchiveobjectwithfile(file)         return result as? nsarray     } } 

and here implimentation of saving , retreiving array

class viewcontroller: uiviewcontroller{   var arraysymptom = [symptomsmodel]()    override func viewdidload() {     super.viewdidload()     arraysymptom = loadarray()      //saving data in device     persistencemanager.savensarray(arraysymptom, key: "symptom")      //loading data device        if let value = persistencemanager.loadnsarray("symptom") as? [symptomsmodel]  {             let images = value[0].images             print("images : \(images)")             let slug = value[0].slug             print("slug : \(slug)")        }                 } 

here able value of slug not able fetch images value. might happening because slug of string type , images of custom class type . please suggest me way can done . is possible save these type of arrays nskeyedarchiver , can access images value retreiving arraysymptom device.

silly mistake was

i getting nil in images because decoded wrong key , copy paste mistake

the error in function..

required init(coder unarchiver: nscoder) { super.init() name = unarchiver.decodeobjectforkey(keys.name) as? string slug = unarchiver.decodeobjectforkey(keys.slug) as? string self.images =  unarchiver.decodeobjectforkey(keys.slug) as? [sym_images] self.videos =  unarchiver.decodeobjectforkey(keys.slug) as? [sym_videos] 

}

and correct decoding must

self.images = unarchiver.decodeobjectforkey(keys.images) as? [sym_images]

self.videos = unarchiver.decodeobjectforkey(keys.videos) as? [sym_videos]


Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

python 3.5 - Pyqtgraph string in x tick -