ios - NSKeyedArchiver Custom Object Array -


note: xcode 8 beta 6

i not sure missing reason getting following error on nskeyedarchiver.archiveddata line.

'nsinvalidargumentexception', reason: '-[_swiftvalue encodewithcoder:]: unrecognized selector sent instance 0x60000024c690' *** first throw call stack: 

here class conforms nscoding protocol:

enum phrasetype {     case create     case item }  class phrase: nsobject, nscoding {      var englishphrase :string!     var translatedphrase :string!     var phrasetype :phrasetype! = phrasetype.item      required init?(coder decoder: nscoder) {          self.englishphrase = decoder.decodeobject(forkey: "englishphrase") as! string         self.translatedphrase = decoder.decodeobject(forkey: "translatedphrase") as! string         self.phrasetype = decoder.decodeobject(forkey: "phrasetype") as! phrasetype     }      func encode(with coder: nscoder) {          coder.encode(self.englishphrase, forkey: "englishphrase")         coder.encode(self.translatedphrase, forkey: "translatedphrase")         coder.encode(self.phrasetype, forkey: "phrasetype")     }      init(englishphrase :string, translatedphrase :string) {          self.englishphrase = englishphrase         self.translatedphrase = translatedphrase          super.init()     }  } 

and here code archiving:

 let userdefaults = userdefaults.standard         var phrases = userdefaults.object(forkey: "phrases") as? [phrase]          if phrases == nil {             phrases = [phrase]()         }          phrases?.append(phrase)          let phrasesdata = nskeyedarchiver.archiveddata(withrootobject: phrases)          userdefaults.set(phrasesdata, forkey: "phrases")         userdefaults.synchronize() 

any ideas?

you can't encode swift enum such phrasetype. instead, give enum raw value , encode raw value (and on decode, use reconstruct correct enum case).

when you've done that, you'll need cast swift array nsarray archived properly.


Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -