1

I've been trying to save an array of objects into NSUserDefaults.

This doesn't seem to be working unfortunately. The array stays empty even though it shouldn't be.

This is the code (All of this is inside a button):

// Load data
NSData *arrayData = [[NSUserDefaults standardUserDefaults]
                     objectForKey:@"profileArray"];
NSMutableArray *profileArray = [NSKeyedUnarchiver unarchiveObjectWithData:arrayData];

// Check if empty
if(!profileArray || !profileArray.count){
    NSMutableArray *temp_array;
    [temp_array addObject:profile_object];
    arrayData = [NSKeyedArchiver archivedDataWithRootObject:temp_array];
    [[NSUserDefaults standardUserDefaults] setObject:arrayData forKey:@"profileArray"];
    printf("Done this");
} else {

    //Save

    arrayData = [NSKeyedArchiver archivedDataWithRootObject:profileArray];
    [[NSUserDefaults standardUserDefaults] setObject:arrayData forKey:@"profileArray"];

    for(CCProfileObject *profile in profileArray){
        printf("%s \n",[profile.getName UTF8String]);
    }

I am unsure if there is a problem here or in the part where I implement the custom encoding/decoding (not visible here). Let me know.

G. Ramistella
  • 1,327
  • 1
  • 9
  • 19

0 Answers0