How to store a custom object to user defaults in objective c?

883 views Asked by At

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?

1

There are 1 answers

0
Rui Peres On BEST ANSWER

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:.