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"];
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];
Raheel sadiq
source share