Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 07-01-2010, 12:39 PM   #1 (permalink)
Registered Member
 
Whitehk's Avatar
 
Join Date: Feb 2010
Posts: 119
Whitehk is on a distinguished road
Default NSUserDefaults not saving?

Hi. I'm saving an NSInteger using NSUserDefaults, and it doesn't seem to be saving it. Every time I kill the app and go back in, it will go back to zero. The idea of it is that the user will click a button and numberOfTimesPressed (the NSInteger) will increase by 1. Here is how I save it:

Code:
-(IBAction)buttonPressed {
	numberOfTimesPressedValue += 1;
	
	NSUserDefaults *numberOfTimesPressedSaved = [NSUserDefaults standardUserDefaults];
	[numberOfTimesPressedSaved setInteger:numberOfTimesPressedValue forKey:@"numberOfTimesPressed"];
	
	label.text = [NSString stringWithFormat:@"%d", numberOfTimesPressedValue];
}
Here is how I retrieve it when the user kills the app then comes back:

Code:
-(void)viewDidLoad {
	[super viewDidLoad];
	
	NSUserDefaults *numberOfTimesPressedSaved = [NSUserDefaults standardUserDefaults];
	numberOfTimesPressedValue = [numberOfTimesPressedSaved integerForKey:@"numberOfTimesPressed"];
	
	label.text = [NSString stringWithFormat:@"%d", numberOfTimesPressedValue];
}
This viewDidLoad method is in the same .m file as the IBAction. Any ideas as to why it wouldn't be saving? Thanks in advance.
Whitehk is offline   Reply With Quote
Old 07-01-2010, 01:58 PM   #2 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by Whitehk View Post
Hi. I'm saving an NSInteger using NSUserDefaults, and it doesn't seem to be saving it. Every time I kill the app and go back in, it will go back to zero. The idea of it is that the user will click a button and numberOfTimesPressed (the NSInteger) will increase by 1. Here is how I save it:

Code:
-(IBAction)buttonPressed {
	numberOfTimesPressedValue += 1;
	
	NSUserDefaults *numberOfTimesPressedSaved = [NSUserDefaults standardUserDefaults];
	[numberOfTimesPressedSaved setInteger:numberOfTimesPressedValue forKey:@"numberOfTimesPressed"];
	
	label.text = [NSString stringWithFormat:@"%d", numberOfTimesPressedValue];
}
Here is how I retrieve it when the user kills the app then comes back:

Code:
-(void)viewDidLoad {
	[super viewDidLoad];
	
	NSUserDefaults *numberOfTimesPressedSaved = [NSUserDefaults standardUserDefaults];
	numberOfTimesPressedValue = [numberOfTimesPressedSaved integerForKey:@"numberOfTimesPressed"];
	
	label.text = [NSString stringWithFormat:@"%d", numberOfTimesPressedValue];
}
This viewDidLoad method is in the same .m file as the IBAction. Any ideas as to why it wouldn't be saving? Thanks in advance.
User defaults are held in memory, and not necessarily written to disk.

Add a call to [ [NSUserDefaults standardUserDefaults] synchronize] every time you change user defaults.

It's worse with iOS 4, because your app doesn't get a terminate message when the user presses the home key, and people usually put their code to save things to disk there.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 07-01-2010, 02:09 PM   #3 (permalink)
Registered Member
 
Whitehk's Avatar
 
Join Date: Feb 2010
Posts: 119
Whitehk is on a distinguished road
Default

Quote:
Originally Posted by Duncan C View Post
User defaults are held in memory, and not necessarily written to disk.

Add a call to [ [NSUserDefaults standardUserDefaults] synchronize] every time you change user defaults.

It's worse with iOS 4, because your app doesn't get a terminate message when the user presses the home key, and people usually put their code to save things to disk there.
That was the problem! Thanks a bunch for both the solution and quick reply!
Whitehk is offline   Reply With Quote
Reply

Bookmarks

Tags
nsinteger, nsuserdefaults, retrieve data, save data, saving nsinteger

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 334
6 members and 328 guests
anothermine, Chickenrig, givensur, michaelhansen, PixelInteractive, Sloshmonster
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,657
Threads: 94,118
Posts: 402,892
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jenniead38
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 12:26 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0