DuskFall
DuskFall

Reputation: 422

NSUserDefaults not loading string array

Before anyone says that its a question thats been asked many times before, i've gone through about 20 different answers and tried everything I found. Despite this, I only started objective c about 5 days ago, so there is probably some really simple solution.

It appears that NSUserDefaults does not load an array from the default property list, despite being called to in many different ways. Before i go on, here is my code for reading the array :

      fishArrayMutable = [[NSMutableArray alloc] initWithArray:(NSArray*)[defaults objectForKey:@"fishArray"] copyItems:TRUE];

     if (fishArrayMutable == nil)
     {
       fishArrayMutable =  [[NSMutableArray alloc] init];
       UIAlertView *alert = [[UIAlertView alloc] init];
       [alert setTitle:@"fishArray = nil"];
       [alert addButtonWithTitle:@"Ok"];
       [alert show];
       [alert release];
     }

And here is my code for writing the array :

[defaults setObject:fishArrayMutable forKey:@"fishArray"];

[defaults synchronize];

This is starting to become a very frustrating problem now, and i've gone about ways of isolating the problem, and reached the conclusion that its in the reading part. The Mutable array is declared in the header file. Please help, this has been bugging me for about 6 hours now...

Upvotes: 2

Views: 899

Answers (2)

zaph
zaph

Reputation: 112857

I suspect that the error is one of:

  1. defaults is not initialized.
  2. The userDefaults are not set prior to reading
  3. the userDefaults are over written prior reading.

Break every step down and write tests before and after each step.

1) NSLog fishArrayMutable where it is written to verify the array contents.
2) Immediatly after writing fishArrayMutable to defaults NSLog the user defaults. Example:

NSLog(@"Before writing fishArray: %@", fishArrayMutable);
[defaults setObject:fishArrayMutable forKey:@"fishArray"];
NSLog(@"After writing fishArray: %@", [[NSUserDefaults standardUserDefaults] objectForKey:@"fishArray"]);

Note that the NSLog statements do not use defaults keeping them self contained.

Do similar NSLog statements bracketing the read (and make the read two statements):

NSLog(@"Before reading fishArray: %@", [[NSUserDefaults standardUserDefaults] objectForKey:@"fishArray"]);
NSArray *fishArray = [defaults objectForKey:@"fishArray"];
NSLog(@"After reading fishArray: %@", fishArray);
fishArrayMutable = [[NSMutableArray alloc] initWithArray:fishArray copyItems:TRUE];
NSLog(@"After creating fishArrayMutable: %@", fishArrayMutable);

Then the problem should be rather obvious.

Note: since the array items themselves will be immutable due to NSUserDefaults instead of:
fishArrayMutable = [[NSMutableArray alloc] initWithArray:fishArray copyItems:TRUE];
you should be able to just use mutableCopy:
fishArrayMutable = [fishArray mutableCopy];

Upvotes: 3

Frade
Frade

Reputation: 2988

Try Using mutableArrayValueForKey check this link

Use it like this:

fishArrayMutable = [[NSMutableArray alloc] initWithArray:[defaults mutableArrayValueForKey:@"fishArray"]];

it works for me

EDIT:

writing:

[defaults setObject:fishArrayMutable forKey:@"fishArray"];
[defaults synchronize];

load:

NSMutableArray *array = [defaults mutableArrayValueForKey:@"fishArray"];

Upvotes: 1

Related Questions