0

I am trying to save an array using NSUserDefaults. I have gotten it to work in other parts of my app and I can't seem to figure out why it doesn't work now. It seems that it is adding (null) to the userDefaults-array, but when I NSLog what I am trying to add ([mainDelegate.globalValdaFragor objectAtIndex:i]) I can see the value I want to input.

The problem seems to be that I can't add anything to the "felPaFragor"-array, seeing as when I try to NSLog it I get (null) and when I tried it with another "random" array it worked.

RPAppDelegate *mainDelegate = (RPAppDelegate *)[[UIApplication sharedApplication]delegate];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

NSMutableArray *felPaFragor = [[NSMutableArray alloc] init];
felPaFragor = [[defaults valueForKey:@"felFragorArray"] mutableCopy];

[felPaFragor addObject:[mainDelegate.globalValdaFragor objectAtIndex:i]];

[defaults setObject:felPaFragor forKey:@"felFragorArray"];

Any help would be greatly appreciated.

Edit: I think I figured it out. It seems I was trying to make a mutableCopy of an empty array and that in turn made my "felPaFragor"-array buggy. Adding this made it work.

felPaFragor = [[NSMutableArray alloc] init];
if ([[defaults valueForKey:@"felFragorArray"] objectAtIndex:0] != NULL) {
    felPaFragor = [[defaults valueForKey:@"felFragorArray"] mutableCopy];
}
7
  • have you NSLog felPaFragor Commented Jan 14, 2013 at 16:10
  • It gives (null). Even if I do it directly after adding an object. Commented Jan 14, 2013 at 16:13
  • Before adding an object, can you insert a NSLog(@"felPaFragor = %@", [felPaFragor description]); and show it's output? Commented Jan 14, 2013 at 16:15
  • That gives: felPaFragor = ( ) or if I put if after I add the mutableCopy: felPaFragor = (null) Commented Jan 14, 2013 at 16:16
  • 1
    I think it is giving nil object and it happens when it doesn't found key in the dictionary Commented Jan 14, 2013 at 16:25

2 Answers 2

1

If after executing this:

NSMutableArray *felPaFragor = [[NSMutableArray alloc] init];
felPaFragor = [[defaults valueForKey:@"felFragorArray"] mutableCopy];

your felPaFragor is nil, then it is highly likely that [defaults valueForKey:@"felFragorArray"] is returning nil.

So, you have a problem with your key, or possibly the object associated to that key has never been stored in NSUserDefaults yet.

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

1 Comment

"or possibly the object associated to that key has never been stored in NSUserDefaults yet." seems to be what was doing it. After adding a check to see if it has been stored before or not it works. Thank you.
1

first: are you using ARC?

if not, you have a memory leak here:

NSMutableArray *felPaFragor = [[NSMutableArray alloc] init];
felPaFragor = [[defaults valueForKey:@"felFragorArray"] mutableCopy];

the first like create and retain a NSMutableArray never released (and never used: you don't need that line at all, ARC or not ARC)

just use:

NSMutableArray *felPaFragor = [[defaults valueForKey:@"felFragorArray"] mutableCopy];

said that, Sergio's answer may be the right one... your valueForKey:@"felFragorArray" may just never been set when you call it at this point

Comments

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.