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 09-14-2010, 12:25 AM   #1 (permalink)
Registered Member
 
Join Date: Sep 2010
Posts: 26
jhockman is on a distinguished road
Default Issues storing and retrieving NSArray

I have a viewController that I want to retrieve an NSMutableArray from userdefaults then under the viewWillDisappear i have it write a new NSArray to userdefaults.

VC.h

NSUserDefaults *defaults;
NSMutableArray *array;

VC.m

viewDidLoad:
defaults = [NSUserDefaults standardUserDefaults];
array = [NSMutableArray arrayWithArray:[defaults arrayForKey:@"CONTACTS"]];

other tableView methods that actually modify the array object.

viewWillDisappear:
[defaults setObject:array forKey:@"CONTACTS"];




There are no errors and the program runs fine the first time the view is loaded. The problem is if I go back to the main view and open it again the array does not get loaded from userdefaults. It just defaults to an array with count of 0. if I NSLog(@"%i", [array count] there's no problem but the minute I try to do any sort of enumerating either with a for(NSObject *object in array) or for(int x =0; x < [array count]; x++) it crashes even though with a count of 0 it should just pass right over the enumeration. Not to mention the fact that the array isn't being saved or loaded properly.

Any ideas?
jhockman is offline   Reply With Quote
Old 09-14-2010, 12:28 AM   #2 (permalink)
Registered Member
 
Join Date: Sep 2010
Posts: 26
jhockman is on a distinguished road
Default

Oh I should add that the array is of NSString objects.
jhockman is offline   Reply With Quote
Old 09-14-2010, 12:29 AM   #3 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

Did you retain "array" in any way? The method arrayWithArray returns an autoreleased array, which will probably be destroyed shortly after viewDidLoad.

Beyond that, try logging array in viewWillDisappear to see if you're getting what you expect.

Code:
NSLog(@"%@",array);
EDIT: Also note that viewDidLoad is not viewWillAppear - the method viewDidLoad might only get run once, or it might run many times if you contantly alloc and dealloc new controllers of this type or if low memory causes the view to get unloaded.
__________________

Free Games!

Last edited by smasher; 09-14-2010 at 12:33 AM.
smasher is offline   Reply With Quote
Old 09-14-2010, 12:42 AM   #4 (permalink)
Registered Member
 
Join Date: Sep 2010
Posts: 26
jhockman is on a distinguished road
Default

I tried that, but I must have messed it up so how because now when I try it it works perfectly. You have got to freaking kidding me lol

Thanks!
jhockman is offline   Reply With Quote
Old 09-14-2010, 12:44 AM   #5 (permalink)
Registered Member
 
Join Date: Sep 2010
Posts: 26
jhockman is on a distinguished road
Default

Oh nevermind. It works the firs two times, but the 3rd time i open the view it crashes.
jhockman is offline   Reply With Quote
Old 09-14-2010, 12:56 AM   #6 (permalink)
Registered Member
 
Join Date: Sep 2010
Posts: 26
jhockman is on a distinguished road
Default

Okay so now I load it from defaults. It loads no problem. But if I try to enumerate it and take one of the NSString objects and compare

if([object intValue] == 5])

it throws an exception saying that [UICGColor intValue] unrecognized selector sent. How is it seeing those objects as UICGColors instead of NSStrings?
jhockman is offline   Reply With Quote
Old 09-14-2010, 01:33 AM   #7 (permalink)
Registered Member
 
Join Date: Sep 2010
Posts: 26
jhockman is on a distinguished road
Default

In case anyone is wondering the final reason was because the second time I moved away from the view it was trying to release the NSUserDefaults object but it had already been release because of the autorelease mechanism. As far as I can tell that's what it was.
jhockman is offline   Reply With Quote
Reply

Bookmarks

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: 336
13 members and 323 guests
Absentia, cgokey, fiftysixty, givensur, heshiming, iGamesDev, linkmx, michaelhansen, mraalex, PixelInteractive, raihan.zbr, Sloshmonster
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,657
Threads: 94,117
Posts: 402,891
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:08 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0