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 08-01-2009, 03:00 PM   #1 (permalink)
Registered Member
 
Join Date: Mar 2009
Posts: 157
Default Mutable Array to NSUserDefaults?!?

So I have some code that save an array in the appwillterminate section and then in the appdidload it pulls it back in. It works great for editing the one item in the array, however, as soon as you start to add objects to the array it crashes. I read something about needed to save a mutable array as a specific "mutable copy" and then it would allow you to add and delete it. My app really depends on me being able to save the state of my nsmutable arrays and then retrieve it. Im having much dificulty doing so though. Has any one gotten this to work? Heres my code:

- (void)applicationDidFinishLaunchingUIApplication *)application {
projectsArray = [[NSUserDefaults standardUserDefaults] objectForKey:@"projectsArray"];
if (projectsArray == NULL) {
projectsArray = [[NSMutableArray arrayWithObjects:@"New Project", nil] retain];
}
}

- (void)applicationWillTerminateUIApplication *)application {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObjectrojectsArray forKey:@"projectsArray"];
// Save data if appropriate
}

Any help would be sweet thanks!!!
starwarsdevwookie59 is offline   Reply With Quote
Old 08-02-2009, 01:48 AM   #2 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
Default

When you copy or save a mutable array, the copy is immutable - so when you're loading, you're getting an immutable array back. You need to make a mutable copy of the array you loaded; then you can change it all you like.
__________________

Free Games!
smasher is offline   Reply With Quote
Old 08-02-2009, 10:06 AM   #3 (permalink)
Registered Member
 
Join Date: Mar 2009
Posts: 157
Default

Quote:
Originally Posted by smasher View Post
When you copy or save a mutable array, the copy is immutable - so when you're loading, you're getting an immutable array back. You need to make a mutable copy of the array you loaded; then you can change it all you like.
Thanks for the reply. I changed my code so it makes it mutable but my app still crashes! Basically i have a table that is filled with a mutable array and when you click the add button it adds one to that array. Anytime i do that and relaunch with this code it quits on me! How do i save nsmutable arrays properly?!?
starwarsdevwookie59 is offline   Reply With Quote
Old 08-02-2009, 10:17 AM   #4 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,129
Default

Well, show what you are doing. How are you making it mutable? Use the code tags.
__________________
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 online now   Reply With Quote
Old 08-02-2009, 10:33 AM   #5 (permalink)
Registered Member
 
Join Date: Mar 2009
Posts: 157
Default

Quote:
Originally Posted by BrianSlick View Post
Well, show what you are doing. How are you making it mutable? Use the code tags.
Got IT!!! Code was fine it was a logic error on my part. I was adding another cell from the first array but it required other data to be added too, not just the cells title. I added the other arrays it uses and its fine! Thanks for all the help!!!
starwarsdevwookie59 is offline   Reply With Quote
Old 11-23-2010, 02:22 AM   #6 (permalink)
JaGDiSH
 
Join Date: Sep 2010
Location: Ahmedabad
Posts: 57
Send a message via Skype™ to jagds
Default NSUserDefaults

- (void)applicationDidFinishLaunchingUIApplication *)application {
projectsArray = [[NSUserDefaults standardUserDefaults] objectForKey:@"projectsArray"];
if (projectsArray == NULL) {
projectsArray = [[NSMutableArray arrayWithObjects:@"New Project", nil] retain];
}
}

- (void)applicationWillTerminateUIApplication *)application {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObjectrojectsArray forKey:@"projectsArray"];
// Save data if appropriate
}


In your code.. What is "projectArray" and "New Array"..?
I am new in iphone development so i dont know how to use NSUserdefaults in code.
I want save array value in it.
jagds is offline   Reply With Quote
Old 11-03-2011, 04:52 AM   #7 (permalink)
Mishti
 
Join Date: Mar 2011
Location: Mumbai
Posts: 28
Default

Quote:
Originally Posted by starwarsdevwookie59 View Post
Got IT!!! Code was fine it was a logic error on my part. I was adding another cell from the first array but it required other data to be added too, not just the cells title. I added the other arrays it uses and its fine! Thanks for all the help!!!
hi,

plz May u give me idea , how to do?

thanx
mishti is offline   Reply With Quote
Reply

Bookmarks

Tags
array, nsmutable, nsuserdefaults

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: 328
22 members and 306 guests
@sandris, ADY, BrianSlick, dacapo, Dani77, djohnson, dre, HDshot, HemiMG, JasonR, MarkC, mer10, nibeck, prchn4christ, ryandb2, spiderguy84, themathminister, timle8n1, tomtom100, vogueestylee, vvenkatachallam
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,883
Threads: 89,229
Posts: 380,763
Top Poster: BrianSlick (7,129)
Welcome to our newest member, vvenkatachallam
Powered by vBadvanced CMPS v3.1.0

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