1

I would like to save an NSArray to NSUSerDefaults. If I do it on Simulator everything works, if I do it on Device, it doesnt work.

NSArray *Test1 = [Daten copy];
NSArray *Test2 = [Kosten copy];

[prefs setObject:Test1 forKey:@"Daten"];
[prefs setObject:Test2 forKey:@"Kosten"];

I am using the above code.

prefs is a normal NSUserDefaults, like NSUserDefaults *prefs = [NSUserDefaults standartUserDefaults];...

Daten & Kosten are Mutable Arrays, to work with.

Everything works on simulator, but on device it doesnt work...

Does anybody have an idea?

0x8badf00d
  • 6,391
  • 3
  • 35
  • 68

3 Answers3

0

Are you synchronizing them after settings/updating ?

[[NSUserDefaults standardUserDefaults] synchronize];
Saurabh
  • 22,743
  • 12
  • 84
  • 133
  • yes its important.. put this after "[prefs setObject:Test2 forKey:@"Kosten"];" and see if you are able to get the data back from nsuserdefaults – Saurabh Dec 10 '11 at 09:21
  • no, nothing changes... damn, why does it work on simulator but not on device?!? – Dennis Weidmann Dec 10 '11 at 09:24
  • well, could it be because of ios5? i am programming with xcode 3.2.5 and ios4 on simulator, but ios5 on device... – Dennis Weidmann Dec 10 '11 at 09:33
  • 1
    In iOS4 or above, your User Defaults may not get saved when pressing the home button. Manually calling [[NSUserDefaults standardUserDefaults] synchronize] in applicationDidEnterBackground: should ensure that your User Defaults are saved correctly (this should really be a built-in behaviour IMO). – Saurabh Dec 10 '11 at 10:32
  • hi, i solved it :-) it was my fault... i made a resigntofirstresponder of a textfield, befor the saving process... so he just didnt save it and resignedtofirstresponder.... the crazy thing is... on ios 4 it worked ;-) thank you for all your help... you can close this or delete it because i think it got no interesting stuff in it :-) – Dennis Weidmann Dec 10 '11 at 10:36
  • it would be great if you can post your answer on SO .. it will help someone someday! – Saurabh Dec 10 '11 at 11:38
0

Because Saurabh asked me to post my solution, I will do it for other readers.

My Problem Code was the following:

[derText1 resignFirstResponder]
[derText2 resignFirstResponder]

NSArray *Test1 = [Daten copy];
NSArray *Test2 = [Kosten copy];

[prefs setObject:Test1 forKey:@"Daten"];
[prefs setObject:Test2 forKey:@"Kosten"];

This worked on an iPhone 4 Simulator with XCode 3.2.5 but not on my Device with iOS 5.x installed.

So I have downloaded and installed the newest XCode version from Apple.

After that it also doesnt work with the Simulator too. It looks like the problem is only with iOS 5 Software.

Than I changed my code simply to that:

NSArray *Test1 = [Daten copy];
NSArray *Test2 = [Kosten copy];

[prefs setObject:Test1 forKey:@"Daten"];
[prefs setObject:Test2 forKey:@"Kosten"];

[derText1 resignFirstResponder]
[derText2 resignFirstResponder]

And it worked on Device and Simulator with the latest iOS. Take a look at the position of the resign First Responder Entrys. They seem to have to be after the saving. I don't know why, but it works.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
0
NSArray *Test1 = [Daten copy];<p>
NSArray *Test2 = [Kosten copy];<p>


NSUserDefaults *prefs;<p>
prefs= [[NSUserDefaults standardUserDefaults]setObject:Test1 forKey:@"Daten"];<p>

[[NSUserDefaults standardUserDefaults] synchronize];

prefs= [[NSUserDefaults standardUserDefaults]setObject:Test2 forKey:@"Kosten"];<p>

[[NSUserDefaults standardUserDefaults] synchronize];
0x8badf00d
  • 6,391
  • 3
  • 35
  • 68
Sirji
  • 554
  • 6
  • 9