2

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...

5
  • What kind of objects do you put into fishArray? Commented Apr 13, 2012 at 10:25
  • Strings, the names of your fish.. Theres also an array called fishSizeArray which is identical Commented Apr 13, 2012 at 10:28
  • I am jumbled here. When are u setting object to defaults? before you allocate it..?? coz you are allocating fishArrayMutable & same time u r asking for that fishArrayMutable stored in defaults. So i am confused..!! Pls help me if I am missing anything Commented Apr 13, 2012 at 10:32
  • No i allocate and read the values in the array in one method, then write them much later, the writing bit is fine, but the reading bit doesnt work, it crashes my app. The writing the array bit comes 4 or 5 method calls down the line. Commented Apr 13, 2012 at 10:34
  • Generally synchronize is not necessary. Commented Apr 13, 2012 at 11:41

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

2 Comments

Did you try loading the array like I indicated?? I load a mutablearray like this and never had problems .. actually is in AppStore
There is no problem, but the class is interesting and not what one would expect: ` NSLog(@"array className: %@", NSStringFromClass([array class]));` is NSKeyValueSlowMutableArray.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.