0

I managed to save an NSMutableArray in NSUserDefaults by first converting it to NSData - I dont deal with a lot of data and just want the data to be there after I switch off & on my phone - but the data does not show up in my table where I would display it. I write the NSUserDefaults back to my array upon loading. Maybe one of you has a hint...? Below the button action where I write to NSUserDefaults and the method viewDidLoad where I write NSUserDefaults to my original array (toDoitems)

- (IBAction)unwindToList:(UIStoryboardSegue *)segue
{
    XYZAddToDoItemViewController *source = [segue sourceViewController];
    XYZToDoItem *item = source.toDoItem;
     if (item !=nil) {
        [self.toDoitems addObject:item];
        NSString *error;
        NSData *data = [NSPropertyListSerialization dataFromPropertyList:self.toDoitems format:NSPropertyListBinaryFormat_v1_0 errorDescription:&error];
        [[NSUserDefaults standardUserDefaults] setObject:data forKey:@"itemArray"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        [self.tableView reloadData];
    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.toDoitems = [[NSMutableArray alloc] init];
    self.toDoitems = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"itemArray"]];
}
NGA
  • 7
  • 1
  • 3
  • Shouldn't this: `[self.tableView reloadData];` be in viewDidAppear in your `XYZAddToDoItemViewController`? And after you set: `self.toDoitems` of course – klcjr89 Jul 16 '14 at 16:15
  • This should answer it as well as close it...possible duplicate of [Storing custom objects in an NSMutableArray in NSUserDefaults](http://stackoverflow.com/questions/537044/storing-custom-objects-in-an-nsmutablearray-in-nsuserdefaults) – danh Jul 16 '14 at 16:50
  • I tried the [self.tableView reloadData]; in viewDidLoad but didnt help. How could I check that there is actually data in NSUserDefaults? – NGA Jul 16 '14 at 17:01

1 Answers1

0

Heres one way to do this

  1. Add encoder decoder functions to your XYZToDoItem class Something like this if say you had 2 strings in this class string1 and string2 : (i havent compiled this code but you get the idea)

    -(void)encodeWithCoder:(NSCoder *)aCoder
    
    {
    [aCoder encodeObject:self.string1 forKey:@"string1"];
    [aCoder encodeObject:self.string2 forKey:@"string2"];
     }
    
    -(id)initWithCoder:(NSCoder *)aDecoder
    {
    
    self = [super init];
    
    if (self) 
    {
        self.string1 = [aDecoder decodeObjectForKey:@"string1"];
        self.string2 = [aDecoder decodeObjectForKey:@"string2"];
    }
    
    return self;
    }
    
  2. Then when you are ready to save do the following

    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:self.myDataArray];
    [userDefaults setObject:data forKey:@"storageName"];
    
    // To load the data from NSUserDefaults
    NSData *myData = [[NSUserDefaults standardUserDefaults] objectForKey:@"storageName"];
    NSArray *temp = (NSMutableArray*)[NSKeyedUnarchiver unarchiveObjectWithData:myData];
    self.myDataArray =  (NSMutableArray*)[temp mutableCopy];
    
hackerinheels
  • 1,141
  • 1
  • 6
  • 14