-1

I am trying to save the NSMutableArray in NSUserDefaults. Here is my code

(void) alarmSetting :(AlarmObject *)alarmObject
{

    [alarmSelect addObject:alarmObject];

    time = alarmObject.clockDate;
    title = alarmObject.titleForAlarm;
    repeat = alarmObject.repeatData;
    video = alarmObject.titleOfTheVideo;
    double uniqueId = alarmObject.uniqueTime;
    ID = [NSNumber numberWithDouble:uniqueId];


    if(![[NSUserDefaults standardUserDefaults] objectForKey:@"MyAlarms"])
    {
        alarmToBeSaved= [[NSMutableArray alloc] init];

    }
    else
    {

        alarmToBeSaved=[[NSUserDefaults standardUserDefaults] valueForKey:@"MyAlarms"] ;

    }

   [alarmToBeSaved addObject:alarmObject];
    [[NSUserDefaults standardUserDefaults] setObject: alarmToBeSaved forKey:@"MyAlarms"];
    NSLog(@"%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"MyAlarms"]);
    [[NSUserDefaults standardUserDefaults] synchronize];

    [tbleView reloadData];

}

But I am getting an error

016-02-25 14:40:05.003 Alarm[3229:87627] Attempt to set a non-property-list object (
    "<AlarmObject: 0x7ff4807ecd80>"
) as an NSUserDefaults/CFPreferences value for key MyAlarms
2016-02-25 14:40:05.054 Alarm[3229:87627] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attempt to insert non-property list object (
    "<AlarmObject: 0x7ff4807ecd80>"
) for key MyAlarms'

why is this so?? I searched this error and I don't understand because I am saving an array in NSUserDefaults.

Muhammad Salman
  • 543
  • 4
  • 22
  • Something that cannot be saved (`AlarmObject`) is in the array. – trojanfoe Feb 25 '16 at 09:50
  • 2
    Possible duplicate of [Attempt to set a non-property-list object as an NSUserDefaults](http://stackoverflow.com/questions/19720611/attempt-to-set-a-non-property-list-object-as-an-nsuserdefaults) – Anbu.Karthik Feb 25 '16 at 09:50
  • Object retrieved from `NSUserDefaults` will be immutable, you will need to get the `mutableCopy`. Second make sure your `AlarmObject ` implements the `NSCoding` protocol. – rckoenes Feb 25 '16 at 10:02

1 Answers1

0

The code you posted tries to save an array of custom objects onto the NSUserDefaults. Of course, You can't do that. Implementing the NSCoding methods doesn't help and you can only store things like NSArray, NSDictionary, NSString, NSData, NSNumber, and NSDate in NSUserDefaults.

First, you need to convert the object to NSData and then store that NSData in NSUserDefaults. You can even store an NSArray of NSData if you need to.

When you read back the array you need to unarchive the NSData to get back your BC_Person objects. This is reverse process of NSKeyedArchiver.

For save

NSUserDefaults *currentDefaults = [NSUserDefaults standardUserDefaults];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:yourObject];
[currentDefaults setObject:data forKey:@"yourKeyName"];

For Get

NSData *data = [currentDefaults objectForKey:@"yourKeyName"];
yourObjectType * token = [NSKeyedUnarchiver unarchiveObjectWithData:data];
For Remove

[currentDefaults removeObjectForKey:@"yourKeyName"];

Hope it helps :)