NSUserDefaults NSObject with an NSArray

I am trying to save an object in NSUserDefaults , looked through a lot of questions on this site, but could not solve the problem, my NSObject has an NSMutableArray another object. for example, the main HotelDC object, and it has an array of "functions", an array of FeactureDC objects.

Here is my code:

 - (id)initWithCoder:(NSCoder *)decoder { self = [[HotelDC alloc] init]; if (self != nil) { self.hotel_id = [decoder decodeIntegerForKey:@"hotel_id"]; self.name = [decoder decodeObjectForKey:@"name"]; self.features = [decoder decodeObjectForKey:@"features"]; } return self; } - (void)encodeWithCoder:(NSCoder *)encoder { [encoder encodeInt:hotel_id forKey:@"hotel_id"]; [encoder encodeObject:name forKey:@"name"]; [encoder encodeObject:features forKey:@"features"];//its a mutable array } 

How do I save and receive it? I get an error like

  Attempt to insert non-property value '<HotelDC: 0xa600fe0>' of class 'HotelDC'. Note that dictionaries and arrays in property lists must also contain only property values. 

Decision:

 //Setting NSData *myEncodedObject = [NSKeyedArchiver archivedDataWithRootObject:hotelObjSelected]; NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; [defaults setObject:myEncodedObject forKey:@"selectHotelObject"]; [[NSUserDefaults standardUserDefaults] synchronize]; // retrieving NSData *data = [defaults objectForKey:@"selectHotelObject"]; hotelObjSelected = [NSKeyedUnarchiver unarchiveObjectWithData:data]; 
+7
source share
4 answers

NSUserDefaults supported by a list of properties. Alas, proprety lists cannot contain serialized objects. Quote from the manual :

The default object should be a list of properties, that is, an instance (or, for collections, a combination of instances): NSData, NSString, NSNumber, NSDate, NSArray or NSDictionary. If you want to save another type of object, you should usually archive it to create an instance of NSData p>

You will need to create your own serialized file data file to directly save the object or serialize the objects as one of the allowed types. Annoyingly, NSUserDefaults does not call encodeWithCoder - it just escapes the type of object passed to setObject:forKey: It is best to either serialize HotelDC fields yourself, or archive the object into an instance of NSData and save it.

+3
source

I did it as follows. Take a look. Below code is in a loop.

  NSMutableArray *newEventArray = [[NSMutableArray alloc] init]; [newEventArray addObject:title]; [newEventArray addObject:alarmDate]; NSArray *iCalAlarmArray = [[NSUserDefaults standardUserDefaults] arrayForKey:@"alarmList"]; if(iCalAlarmArray == nil || [iCalAlarmArray count] <= 0) { iCalAlarmArray = [[NSMutableArray alloc] init]; } iCalAlarmArray = [iCalAlarmArray arrayByAddingObject:newEventArray]; [[NSUserDefaults standardUserDefaults] setObject:iCalAlarmArray forKey:@"alarmList"]; [[NSUserDefaults standardUserDefaults] synchronize]; 

May this help you.

+1
source

you should write similar encoding and decoding methods in FeatureDC and store them in an array and then encode here.

0
source

The default object should be a list of properties, that is, an instance (or, for collections, a combination of instances): NSData, NSString, NSNumber, NSDate, NSArray or NSDictionary. If you want to save some other type of object, you should usually archive it to create an instance of NSData p>

0
source

All Articles