0

I have strange problem with NSUserDefaults.

I would like to save NSMutableArray.

I have this piece of code:

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:selected forKey:@"markListArr"];

The problem is that the object for key "markListArr" doesn't get saved. I do this at the first run of the app. When later in the app I want to save object for that key everything works fine.

If I use any other key everything works fine. I would like to use that specific key because I already have app on app store and this is only update to the existing app.

I already tried [userDefaults synchronize] and it doesn't work.

Any ideas what is happening?

Matic1911
  • 381
  • 1
  • 3
  • 13
  • What do you mean it isn't saved? Have you set a breakpoint? Logged out any information? Does 'selected' exist? – Tim Aug 30 '16 at 20:38
  • How do you retrieve the array? You should call `arrayForKey:` on user defaults. – Ozgur Vatansever Aug 30 '16 at 20:46
  • Possible duplicate of [Saving NSArray to NSUserDefaults and getting it in NSMutableArray](http://stackoverflow.com/questions/9212430/saving-nsarray-to-nsuserdefaults-and-getting-it-in-nsmutablearray) – Aviram Aug 30 '16 at 20:51
  • 1
    What's in the array? Is it [key-value coding compliant](https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/KeyValueCoding/Articles/KeyValueCoding.html)? Are you persisting the user default values before you shut down the app with `[[NSUserDefaults standardUserDefaults] synchronize];`? – Marcus Adams Aug 30 '16 at 20:56
  • Yes I did everything you guys suggested. The problem is that it does not work with that specific key only. If I save with another key it works. I have that code in a completion block of the request. In the array are int values. – Matic1911 Aug 30 '16 at 21:19
  • I did debug with `NSLog(@"%@", [[NSUserDefaults standardUserDefaults] dictionaryRepresentation])` and the problem is that the key and the array are not saved. – Matic1911 Aug 30 '16 at 21:26
  • Please provide more information: where is that "piece of code" called? What method? How are you creating or obtaining the value in `selected`? (let's see the code for that). My guess is that `selected` ends up being `nil` in the code above because, for instance, you're trying to query the UI before the nib is fully awoken (and your `IBOutlet`s are still `nil`)... – NSGod Aug 30 '16 at 22:10
  • It is called in a completion block of a request. This is the code for creating array. `NSMutableArray *selected = [[NSMutableArray alloc] init]; for (int i = 0; i < answers.count; i++) { int answerPosition = [answers[i][@"position"] intValue]; [selected addObject:[NSNumber numberWithInteger:answerPosition - 1]]; }` Array is not nil I have checked that with the debugger. – Matic1911 Aug 31 '16 at 09:56

1 Answers1

0

Are you calling [userDefaults synchronize] before fetching the object? I'm using the code below:

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSMutableArray *selected = [[NSMutableArray alloc] init];
[selected addObject:@"New Value"];
[userDefaults setObject:selected forKey:@"markListArr"];
[userDefaults synchronize];

NSMutableArray *arr = [userDefaults objectForKey:@"markListArr"];
NSLog(@"arr: %@", arr);

and it prints:

arr: (
    "New Value"
)
Alex Koshy
  • 1,613
  • 15
  • 17
  • Yes I have exactly like that. I have this code in the completion block of the request. The problem is that I save three different things in the user defaults and that only this specific key does not work. Every other key works fine. – Matic1911 Aug 30 '16 at 21:21
  • I did debug with `NSLog(@"%@", [[NSUserDefaults standardUserDefaults] dictionaryRepresentation])` and the problem is that the key and the array are not saved. – Matic1911 Aug 30 '16 at 21:26
  • Are all of the keys in the mutable array NSStrings? You may have trouble saving the mutable array if it is not NSCoding compliant. Maybe this post will help if you haven't already tried? http://stackoverflow.com/a/471920/2845237 – Alex Koshy Aug 30 '16 at 21:54
  • Tried now and it doesn't work. All objects in array are of type `NSNumber`. I can't figure it out why is the problem with specific key only. – Matic1911 Aug 31 '16 at 09:53