Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSUserDefaults NSObject with NSArray Object

I am trying to save object in NSUserDefaults, went throught many questions on this site but could not resolve the issue, my NSObject has an NSMutableArray of another object. like here the main object is HotelDC and it has an array "features" 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 should I save it, and retrieve?? I am getting error as

 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.

Solution :

//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];
like image 549
Raheel Sadiq Avatar asked Feb 04 '13 12:02

Raheel Sadiq


2 Answers

NSUserDefaults is backed by a property list. Alas, proprety lists cannot contain serialised objects. Quoting from the manual:

A default object must be a property list, that is, an instance of (or for collections a combination of instances of): NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary. If you want to store any other type of object, you should typically archive it to create an instance of NSData

You'll have to create your own serialised data file for saving the object directly, or serialise the objects as one of the allowed types. Annoyingly, NSUserDefaults doesn't call encodeWithCoder - it just screens the object type passed to setObject:forKey:. The best bet is to either serialise the fields of the HotelDC yourself, or archive the object to an NSData instance and store that.

like image 51
Adam Wright Avatar answered Oct 21 '22 22:10

Adam Wright


I have did this by following way.check it. Below code is in for 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 helps you.

like image 38
python Avatar answered Oct 21 '22 22:10

python