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-09-2011, 08:12 AM   #1 (permalink)
Flash Developer
 
Join Date: Mar 2011
Location: Norway
Posts: 77
thh022 is on a distinguished road
Default NSUserDefault

My first try using the NSUserDefault where the user can save their own favourites in. I use a NSMutableArray to store the NSUserDefault-objects in, but I can't get it quite right:

Code:
NSMutableArray *tempArray = [[NSMutableArray alloc] init];

NSUserDefaults *temp1 = [NSUserDefaults standardUserDefaults];
[temp1 setObject:@"string 1" forKey:@"name"];
[temp1 synchronize];
[tempArray addObject:temp1];

NSUserDefaults *temp2 = [NSUserDefaults standardUserDefaults];
[temp2 setObject:@"string 2" forKey:@"name"];
[temp2 synchronize];
[tempArray addObject:temp2];

for(int i=0; i<[tempArray count]; i++){
   NSUserDefaults *tempNSUserDefaults = [tempArray objectAtIndex:i];
   NSLog(@"Test: %@", [tempNSUserDefaults stringForKey:@"name"]);
}
The output is:

Test: string 2
Test: string 2

It seems like the second NSUserDefault overwrite the first one. I'm am missing something?
__________________
http://www.rabaldus.no
thh022 is offline   Reply With Quote
Old 09-09-2011, 08:49 AM   #2 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

Why are you adding NSUserDefaults objects to the array? Defaults are a singleton object, and you shouldn't be retaining them anywhere.
baja_yu is offline   Reply With Quote
Old 09-09-2011, 10:09 AM   #3 (permalink)
Registered Member
 
SundialSoft's Avatar
 
Join Date: Oct 2010
Location: Scotland
Posts: 176
SundialSoft is on a distinguished road
Default

Quote:
Originally Posted by thh022 View Post
My first try using the NSUserDefault where the user can save their own favourites in. I use a NSMutableArray to store the NSUserDefault-objects in, but I can't get it quite right:

Code:
NSMutableArray *tempArray = [[NSMutableArray alloc] init];

NSUserDefaults *temp1 = [NSUserDefaults standardUserDefaults];
[temp1 setObject:@"string 1" forKey:@"name"];
[temp1 synchronize];
[tempArray addObject:temp1];

NSUserDefaults *temp2 = [NSUserDefaults standardUserDefaults];
[temp2 setObject:@"string 2" forKey:@"name"];
[temp2 synchronize];
[tempArray addObject:temp2];

for(int i=0; i<[tempArray count]; i++){
   NSUserDefaults *tempNSUserDefaults = [tempArray objectAtIndex:i];
   NSLog(@"Test: %@", [tempNSUserDefaults stringForKey:@"name"]);
}
The output is:

Test: string 2
Test: string 2

It seems like the second NSUserDefault overwrite the first one. I'm am missing something?
NSUserDefaults is where you store various data and then recover it from there. You store them like this
Code:
       [[NSUserDefaults standardUserDefaults] setObject:toDoStates forKey:@"toDoStates"];
(where toDoStates is an NSArray)
and retrieve them like this
Code:
    NSArray* toDoStates = [[NSUserDefaults standardUserDefaults] objectForKey:@"toDoStates"];
Have a look for posts about this topic as there are various nuances depending on what you want to save.
SundialSoft is offline   Reply With Quote
Old 09-11-2011, 03:17 AM   #4 (permalink)
Flash Developer
 
Join Date: Mar 2011
Location: Norway
Posts: 77
thh022 is on a distinguished road
Default

I thought I could save them in an array. The reason why I tried it this way is that the app is having a list of names and the users can set the names they want as a favourite. I need to save these names someway and the list could be from 0 until 50 names so it has to by dynamic.

Any suggestions how to do this?
__________________
http://www.rabaldus.no
thh022 is offline   Reply With Quote
Old 09-11-2011, 04:58 AM   #5 (permalink)
Registered Member
 
SundialSoft's Avatar
 
Join Date: Oct 2010
Location: Scotland
Posts: 176
SundialSoft is on a distinguished road
Default

the example I posted is saving an array!
SundialSoft is offline   Reply With Quote
Old 09-12-2011, 05:44 AM   #6 (permalink)
Flash Developer
 
Join Date: Mar 2011
Location: Norway
Posts: 77
thh022 is on a distinguished road
Default

Ahh, sorry that I didnīt see that.

Thanks, now I got it to work!

In my case I use an array for saving names. Only hazzle to use arrays with NSUserDefaults is that it returns an NSArray and not an NSMutableArray.

Hereīs a couple of good readings for those who want to use NSUserDefaults:

iPhone Development 101: User Defaults
CocoaDev: NSUserDefaults
__________________
http://www.rabaldus.no

Last edited by thh022; 09-14-2011 at 01:57 PM.
thh022 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: 414
11 members and 403 guests
apatsufas, Eclectic, fiftysixty, JackReidy, jeroenkeij, Sami Gh, teebee74, tim0504, UMAD, yomo710, yuncarl28
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,672
Threads: 94,121
Posts: 402,904
Top Poster: BrianSlick (7,990)
Welcome to our newest member, yuncarl28
Powered by vBadvanced CMPS v3.1.0

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