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

Mockup & CodeGen, iPhone & iPad
($9.99)

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

Manu
($0.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-30-2009, 12:26 AM   #1 (permalink)
Registered Member
 
Join Date: Mar 2009
Posts: 157
Default App Works Great... Now to User Defaults?

Hey,
I have an App that is used for budgeting. Basically it adds costs as objects in Arrays and puts them in table views. So heres the problem. Ive got a lot of arrays, like a whole freaking lot. How do I save them all to the user defaults and then restore them when the app reloads? Im guessing its pretty complex... Anybody want to point me in the right direction? Thanks! : )
starwarsdevwookie59 is offline   Reply With Quote
Old 07-30-2009, 01:04 AM   #2 (permalink)
Registered Member
 
Join Date: Jul 2009
Location: North America
Posts: 50
Default nsuserdefaults

it shouldn't be that complicated. First of all you should make sure you really need to save all that information. I'm guessing you are.
NSUserDefaults isn't so hard to use at all. In your controller you need an ivar

NSUserDefaults* userDefaults

Inside awakeFromNib I used the following line:

userDefaults = [[NSUserDefaults standardUserDefaults] retain];

this retrieves any saved data and saves it when the app quits (I think...)

Now the way you save and retrieve data is using a NSString key, like @"myArray". Then you say something like: NSArray* array = [userDefaults objectForKey @"myArray"]; this will retrieve the array in question.

At some point you have to save the data. You can either do it every time you make a change or at the very end of the program (i.e. in the terminated app method... however if the app crashes or quits unexpectedly this isn't always called). Anyhoo you just want to save it like so:

[userDefaults setObject: array forKey: @"myArray"]; this saves the array in a set block of memory under the key "myArray" (key can be anything really, just don't lose your key! )

There is lots more detail in the apple documentation.
shimmy is offline   Reply With Quote
Old 07-30-2009, 01:27 AM   #3 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,129
Default

I don't think it is necessary to retain NSUserDefaults. And it wouldn't hurt to include the type declaration:

Code:
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
__________________
BriTer Ideas LLC - Code review, consulting, development. PM for pricing.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
BrianSlick is offline   Reply With Quote
Old 07-30-2009, 01:34 AM   #4 (permalink)
Registered Member
 
Join Date: Jul 2009
Location: North America
Posts: 50
Default

Quote:
Originally Posted by BrianSlick View Post
I don't think it is necessary to retain NSUserDefaults. And it wouldn't hurt to include the type declaration:

Code:
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
I think I just saw that in an example somewhere. I'll have to try without retaining...

I wrote the type declaration, didn't I? Are you suggesting something different? Where would you put that code?
shimmy is offline   Reply With Quote
Old 07-30-2009, 01:42 AM   #5 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,129
Default

Code:
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
That part.

I'm not positive, but NSUserDefaults is most likely a singleton, in which case retain/release shouldn't have any effect. Easy enough to find out, I guess.



Oooooh, wait... I misread what you wrote.

It is not necessary to set an ivar for the defaults. Just use the line I gave anytime you need to grab a hold of the defaults.
__________________
BriTer Ideas LLC - Code review, consulting, development. PM for pricing.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
BrianSlick is offline   Reply With Quote
Old 07-30-2009, 05:57 PM   #6 (permalink)
Registered Member
 
Join Date: Mar 2009
Posts: 157
Default

Alright! Ive got some code thats working, kinda. Here it is:

To Load at Start:
projectsArray = [[NSUserDefaults standardUserDefaults] objectForKey:@"projectsArray"];
if (projectsArray == NULL) {
projectsArray = [[NSMutableArray arrayWithObjects:@"New Project", nil] retain];
}
To Save When App Quits:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObjectrojectsArray forKey:@"projectsArray"];

It works great for editing the first object in the array but crashes if the array has more than one object! also, does xcode save the defaults? because it seems like my programs crash even when i revert them to code that did work because its remembering the bad code i had saved to the defaults before! How do i clear user defaults so that it builds and installs the app as if its brand new?!?
starwarsdevwookie59 is offline   Reply With Quote
Old 07-30-2009, 06:41 PM   #7 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,129
Default

See this post about the location:

http://www.iphonedevsdk.com/forum/ip...tml#post106575

You can clear out that applications folder, or in the simulator you can "Reset Contents And Settings..." which will do the same thing.

When pulling data out of NSUserDefaults like this, the result is not mutable - so NSArray vs. NSMutableArray - even if you put a mutable object into it. That could be related to the crash you are seeing, and something to keep in mind for elsewhere in your program when you are trying to do things with the array.
__________________
BriTer Ideas LLC - Code review, consulting, development. PM for pricing.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
BrianSlick is offline   Reply With Quote
Old 08-01-2009, 12:08 PM   #8 (permalink)
Registered Member
 
Join Date: Mar 2009
Posts: 157
Default

I read something about making it a mutable copy? But i cant figure it out...
starwarsdevwookie59 is offline   Reply With Quote
Reply

Bookmarks

Tags
array, defaults, save

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: 251
24 members and 227 guests
ADY, AragornSG, bookesp, chillyh, dacapo, Dani77, Davey555, Dominus, dre, glenn_sayers, HemiMG, JasonR, karlam963, LEARN2MAKE, M.A.S., marshusensei, mer10, nobre84, Oral B, prchn4christ, Raggou, Rudy, spiderguy84, themathminister
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,885
Threads: 89,230
Posts: 380,765
Top Poster: BrianSlick (7,129)
Welcome to our newest member, bookesp
Powered by vBadvanced CMPS v3.1.0

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