1

I have a custom Swift Class which writes data to the file system using NSKeyedArchiver. One of the functions in this class to do the job is:

func writeDataToArchive (data: AnyObject) {

    let dirPath = "\(currentPath)/\(fileName)"

    //----

    NSKeyedArchiver.archiveRootObject(data, toFile: dirPath)


}

I then call this function like so, by passing it a piece of data to save:

let dataToWrite: [NoteItem] = Hub.allNotes

metaData.writeDataToArchive(dataToWrite)

When I run my program, Xcode is crashing at this line

NSKeyedArchiver.archiveRootObject(data, toFile: dirPath)

with the following Error Message:

2015-10-12 05:25:39.949 MyApp[86437:4755070] *** NSForwarding: warning: object 0x7fe5ba5533c0 of class 'MyApp.NoteItem' does not implement methodSignatureForSelector: -- trouble ahead Unrecognized selector -[MyApp.NoteItem replacementObjectForKeyedArchiver:]

Based on the error message it seems to be an issue with something about trying to write my Custom Object to the device. When I use other standard Data Types like Int, String etc...they work fine, so it seems to be an issue with using a Custom Object. How do I fix this?

Lavvo
  • 1,024
  • 3
  • 16
  • 35
  • How is `NoteItem` defined? – ljk321 Oct 12 '15 at 09:48
  • Note Item has a bunch of variables and functions inside of it. I think I may have actually solved my problem, was doing additional research and found this excellent post, so far it has fixed the crash. Still trying to retreieve the result to confirm it works http://stackoverflow.com/questions/32536227/converting-custom-class-object-into-nsdata – Lavvo Oct 12 '15 at 10:01
  • That's cool. I'm thinking you might missing `NSCoding` when you implements your own model. – ljk321 Oct 12 '15 at 10:03
  • Yes, in a nutshell, I was missing NSCoding and the required protocols. Everything is working fine now after doing a quick test. – Lavvo Oct 12 '15 at 10:05

1 Answers1

3

If you haven't already, make sure your object inherits from NSObject per this previous post.

Community
  • 1
  • 1
Julian
  • 2,837
  • 17
  • 15
  • Ii found this post fixed my problem http://stackoverflow.com/questions/32536227/converting-custom-class-object-into-nsdata – Lavvo Oct 12 '15 at 10:07