How to properly pass NSMutableArray to NSUserDefaults using a custom data model?

Let me preface that my code was hidden a bit on purpose. I know that the name will not be good. I'm trying to add a widget to my application with new iOS 8 functionality. I use this link as a tutorial

Now, while I have it in my ViewController, when my submit button is called in my application. By this time, I have all my data in this array to be transmitted.

//Passes the array of times to the shared group to be called by the notification center widget. NSUserDefaults *sharedDefaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.myGroup.TodayExtensionSharingDefaults"]; NSData *encodedArray = [NSKeyedArchiver archivedDataWithRootObject:tableViewController.TimeArray]; [sharedDefaults setObject:encodedArray forKey:@"TimesArray"]; [sharedDefaults synchronize]; 

However, my array stores the user data model that I created, so in my data model I have:

 - (void)encodeWithCoder:(NSCoder *)encoder { //Encode properties, other class variables, etc [encoder encodeInt:_stopNumber forKey:@"stopNumber"]; [encoder encodeObject:_route forKey:@"route"]; [encoder encodeObject:_number forKey:@"number"]; [encoder encodeInt:_time forKey:@"time"]; [encoder encodeObject:_minutesOrApproaching forKey:@"@minutesOrApproaching"]; [encoder encodeObject:_noPrediction forKey:@"noPrediction"]; } - (id)initWithCoder:(NSCoder *)decoder { self = [super init]; if(self) { //decode properties, other class vars _stopNumber = [decoder decodeIntForKey:@"stopNumber"]; _route = [decoder decodeObjectForKey:@"route"]; _number = [decoder decodeObjectForKey:@"number"]; _time = [decoder decodeIntForKey:@"time"]; _minutesOrApproaching = [decoder decodeObjectForKey:@"minutesOrApproaching"]; _noPrediction = [decoder decodeObjectForKey:@"noPrediction"]; } return self; } 

And then in my widget I call this code:

 NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.myGroup.TodayExtensionSharingDefaults"]; NSData *myDecodedObject = [defaults objectForKey: @"TimesArray"]; NSArray *decodedArray =[NSKeyedUnarchiver unarchiveObjectWithData: myDecodedObject]; int i = 0; for (ETA *eta in decodedArray) { if(i == 0){ _stopNumber.text = eta.number; } UILabel *label = [timeSlots objectAtIndex:i]; label.text = eta.minutesOrApproaching; i++; } 

My problem is that I keep getting:

* Application termination due to an undetected exception "NSInvalidUnarchiveOperationException", reason: "* - [NSKeyedUnarchiver decodeObjectForKey:]: cannot decode class object (ETA) 'enter the code here

So, I am a bit staged because I don’t know what I am doing wrong here. It would be great if someone could help me point in the right direction. Thanks again!

-1
source share
1 answer

So it turns out that custom classes can be sick. And although all the code seems to be correct, I still get the error. So I stepped back and looked at my custom object and asked myself: "What do I really need from this?" and using this process I decided that I need only certain values, so I encrypted only those values, not the object. Since these values ​​were ints and strings, they are built into iOS in how to encode and decode. From there, I was able to convey the values ​​around thanks to @meda's answer and my idea.

+1
source

Source: https://habr.com/ru/post/1206429/


All Articles