2

I tried converting my objects to instances of NSData, putting them in an NSArray and storing the array in user defaults. I also tried putting the objects in an NSArray, converting the array to an instance of NSData and storing it in user defaults. Both ways I get the following error:

"Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[GelirObject encodeWithCoder:]: unrecognized selector sent to instance 0x89dbf90" The name of my custom object is GelirObject.

I know that custom objects cannot be stored to user defaults directly. That's why I used NSData, but still I get this error. How can I store my objects without getting error?

BergQuester
  • 6,167
  • 27
  • 39
user3113020
  • 33
  • 1
  • 3
  • 1
    I don't think you correctly serialized the object into an NSData. Can you show how you tried to do that? – Chuck Dec 17 '13 at 21:56
  • 1
    It could be that you're assigning an object of type `GelirObject*` to a variable of type `NSData*`, which you then try to save to the user defaults. – TotoroTotoro Dec 17 '13 at 22:00
  • I serialized this way: NSData *incomeObjectData = [NSKeyedArchiver archivedDataWithRootObject:incomeObject]; I already checked the post Storing data to NSUserDefaults but I couldn't understand why I got the error – user3113020 Dec 17 '13 at 22:16
  • @user3113020: Your object needs to implement `encodeWithCoder:` as shown in the other question. – Chuck Dec 17 '13 at 22:28

1 Answers1

0

Well first of all, it's just wrong what you are doing. You shouldn't put a lot of information inside your NSUserDefaults. As for the issue:

NSData *incomeObjectData = [NSKeyedArchiver archivedDataWithRootObject:incomeObject];

Your GelirObject doesn't comply with the NSCoding protocol. That's the reason your app is screaming. Just implement the required methods initWithCoder: and encodeWithCoder:.

Rui Peres
  • 25,741
  • 9
  • 87
  • 137