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];

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.


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.


您应该在FeatureDC中编写类似的编码和解码方法,并将其存储在数组中,然后在此进行编码。

链接地址: http://www.djcxy.com/p/69130.html

上一篇: UML状态机,本地与外部转换

下一篇: 使用NSArray对象的NSUserDefaults NSObject