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 01-29-2011, 06:14 PM   #1 (permalink)
Just Some Guy
 
Join Date: Dec 2010
Posts: 112
UnretroGamer is on a distinguished road
Arrow NSUserDefaults: Remove all keys and Save all keys

This is a two part question:
  1. How would I save all keys in [NSUserDefaults standardUserDefaults]? To save all settings.
  2. How would I remove all keys from [NSUserDefaults standardUserDefaults]? To reset all settings.

Thank you in advance! ^.^

EDIT: This is so I can have multiple user profiles. I may need to add all keys but one key, to a key for this to work the way I plan. There may also be a completely different/better way of doing this, making user profiles.

Last edited by UnretroGamer; 01-29-2011 at 06:20 PM.
UnretroGamer is offline   Reply With Quote
Old 01-29-2011, 07:44 PM   #2 (permalink)
Just Some Guy
 
Join Date: Dec 2010
Posts: 112
UnretroGamer is on a distinguished road
Default

This is probably not how it should be done so let me write a new question:

How would I set up different user profiles using different keys?

Maybe a key for each profile, then the question would be: How do I add a bunch of keys to a key or something.

I think you guys get my idea, how do I have it so each User Profile has his own keys?
UnretroGamer is offline   Reply With Quote
Old 01-29-2011, 08:13 PM   #3 (permalink)
Just Some Guy
 
Join Date: Dec 2010
Posts: 112
UnretroGamer is on a distinguished road
Default

While writing another post. I looked at NSArray and I'm guessing I could just assign all settings to an NSArray and then put those NSArrays in a NSUserDefaults key for each profile. So for 3 User profiles I would have 3 NSUserDefaults keys holding an array of settings in each key.

The question has now changed. I don't know much about NSArrays so how would I store and retrieve settings in an array. I heard NSMutableArrays don't play nice with memory management.
UnretroGamer is offline   Reply With Quote
Old 01-29-2011, 08:18 PM   #4 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 6
jaxjason is on a distinguished road
Default

Quote:
Originally Posted by UnretroGamer View Post
While writing another post. I looked at NSArray and I'm guessing I could just assign all settings to an NSArray and then put those NSArrays in a NSUserDefaults key for each profile. So for 3 User profiles I would have 3 NSUserDefaults keys holding an array of settings in each key.

The question has now changed. I don't know much about NSArrays so how would I store and retrieve settings in an array. I heard NSMutableArrays don't play nice with memory management.
I am more of a novice but got everything for using nsarray right from google and apple docs. I literally just typed in "nsarray example" and got back lots of good samples and explanations.

The mutables are for things that can change in size, more or less elements. Since your setting user settings type data, the should be a set number of items. So nsarray should work fine.

Jason
jaxjason is offline   Reply With Quote
Old 01-29-2011, 08:23 PM   #5 (permalink)
Just Some Guy
 
Join Date: Dec 2010
Posts: 112
UnretroGamer is on a distinguished road
Default

Quote:
Originally Posted by jaxjason View Post
I am more of a novice but got everything for using nsarray right from google and apple docs. I literally just typed in "nsarray example" and got back lots of good samples and explanations.

The mutables are for things that can change in size, more or less elements. Since your setting user settings type data, the should be a set number of items. So nsarray should work fine.

Jason
Ah so I can still edit the values within an NS[nonmutable]Array? ex. Set int theme to 1 and set startGamePaused to YES instead of NO.

EDIT: I'll be adding settings as I go through updates so I'm guessing I can add all the objects from the old one if the existing one doesn't have {key I'll be adding to array} to the new one and delete the old key holding the old NSArray and create a new key of the same name holding the new one.

Last edited by UnretroGamer; 01-29-2011 at 08:28 PM.
UnretroGamer is offline   Reply With Quote
Old 01-29-2011, 08:36 PM   #6 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 6
jaxjason is on a distinguished road
Default

>Ah so I can still edit the values within an NS[nonmutable]Array? ex. Set int theme to 1 and set startGamePaused to YES instead of NO.

I am just an amateur like you at this. Read up on the nsarray Changing values.

I did read that this might help: replaceObjectAtIndex:withObject
so you wouldn't change the item in a location, but replace it with a new item with the new preference setting I think.

Jason
jaxjason is offline   Reply With Quote
Old 01-29-2011, 08:44 PM   #7 (permalink)
Just Some Guy
 
Join Date: Dec 2010
Posts: 112
UnretroGamer is on a distinguished road
Red face

Quote:
Originally Posted by jaxjason View Post
>Ah so I can still edit the values within an NS[nonmutable]Array? ex. Set int theme to 1 and set startGamePaused to YES instead of NO.

I am just an amateur like you at this. Read up on the nsarray Changing values.

I did read that this might help: replaceObjectAtIndex:withObject
so you wouldn't change the item in a location, but replace it with a new item with the new preference setting I think.

Jason
Yeah all these useful functions are for NSMutableArray. I'm just a little hesitant to making a Mutable because I heard mutable means memory hog or something bad like that. It's my only option for now and seems to fit perfectly with functions like removeObjectsIdenticalTo:, removeAllObjects for reset, addObject:, addObjectsFromArray: and replaceObjectAtIndex:withObject:. Thanks so much for the help. Although my mind would feel much more at ease if an experienced programmer confirmed my conclusion.
UnretroGamer is offline   Reply With Quote
Old 01-29-2011, 08:53 PM   #8 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

I see a reset method in the user defaults documentation, although the description sounds a little weird, but that should be the answer to #2.

I'm not sure that #1 is inherently supported in this way, although I see "domain" in the documentation, but I don't actually know what that is. Might be worth exploring.

Arrays should really only be used in cases where order matters. The order of your settings doesn't matter, so you wouldn't want to lock in a particular order by hard-coding an array. This would be a better use of a dictionary, and would keep more in line with how user defaults works (it's essentially a fancy dictionary).

Mutable vs not-mutable does not affect the items IN the collection (array, set, dictionary, etc) in any way. It is a restriction on the collection itself. If you create an immutable array with 10 objects, that array will only ever have 10 objects, and will only ever have those 10 objects. If those objects are Car objects, and those Car objects have a color property, you can change the color of every one of those Car objects in an NSArray. If you want to add, remove, or replace a Car object, you can't. If you need to perform any of those operations, then you need to have a mutable collection.

So an option for #1 is to keep your own dictionary of settings, and then save this dictionary into user defaults, perhaps using the username as a key. You can save and retrieve individual dictionaries per user as needed.

Keep in mind that what comes out of NSUserDefaults is NOT mutable, so you may need to make mutable copies before you finish loading stuff.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

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

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 01-29-2011, 09:00 PM   #9 (permalink)
Just Some Guy
 
Join Date: Dec 2010
Posts: 112
UnretroGamer is on a distinguished road
Default

Quote:
Originally Posted by BrianSlick View Post
I see a reset method in the user defaults documentation, although the description sounds a little weird, but that should be the answer to #2.

I'm not sure that #1 is inherently supported in this way, although I see "domain" in the documentation, but I don't actually know what that is. Might be worth exploring.

Arrays should really only be used in cases where order matters. The order of your settings doesn't matter, so you wouldn't want to lock in a particular order by hard-coding an array. This would be a better use of a dictionary, and would keep more in line with how user defaults works (it's essentially a fancy dictionary).

Mutable vs not-mutable does not affect the items IN the collection (array, set, dictionary, etc) in any way. It is a restriction on the collection itself. If you create an immutable array with 10 objects, that array will only ever have 10 objects, and will only ever have those 10 objects. If those objects are Car objects, and those Car objects have a color property, you can change the color of every one of those Car objects in an NSArray. If you want to add, remove, or replace a Car object, you can't. If you need to perform any of those operations, then you need to have a mutable collection.

So an option for #1 is to keep your own dictionary of settings, and then save this dictionary into user defaults, perhaps using the username as a key. You can save and retrieve individual dictionaries per user as needed.

Keep in mind that what comes out of NSUserDefaults is NOT mutable, so you may need to make mutable copies before you finish loading stuff.
Awesome, just what I needed! Thanks for the millionth time!
UnretroGamer is offline   Reply With Quote
Old 01-29-2011, 10:01 PM   #10 (permalink)
Just Some Guy
 
Join Date: Dec 2010
Posts: 112
UnretroGamer is on a distinguished road
Default

Actually now that I think about it. Another thing is that I MAY be adding new settings and things as I expand the game through updates. So lets say I store all of their (the user of UserProfileX) settings in an NSDictionary (UserProfileX). Would I just

Code:
for (int profile = 0; profile < 3; profile++) {
     NSString *userProfile = [NSString stringWithFormat:@"UserProfile%i", profile];
     NSMutableDictionary *newUserProfile = [NSMutableDictionary dictionaryWithDictionary:userProfile];
     if (![newUserProfile objectForKey:(NSString){name of setting I add}]){
     //Please check doc for NSDictionary objectForKey: because I wonder if aKey is supposed to be an NSString like NSUserDefaults Objects, it says a value.
          [newDictionary removeObjectForKey:(NSString){name of setting I don't use anymore}];
          [newDictionary setObject:(BOOL){boolean setting I add} forKey:(NSString){name of setting I add}];
          [[NSUserDefaults standardUserDefaults] removeObjectForKey:userProfile];
          [[NSUserDefaults standardUserDefaults] setObject:newUserProfile forKey:userProfile];
     }
}
NOTE: It took me a while to write that but I wrote all of it on my own. Look what you've done to me! I set it to three since 3 is the maximum number of profiles. I wonder if only two profiles have been created, whether the app will crash, or do nothing. ^.^


Hopefully you can answer a question I've had in the air for some time. Does a Mutable thing (array, dictionary, whatever) hog more memory than a non-Mutable thing containing the same number of objects? I heard mutable is bad for memory (I forgot where, it may just be an assumption actually) is this true? I feel naughty when I use mutable things.
UnretroGamer is offline   Reply With Quote
Old 01-29-2011, 10:18 PM   #11 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

Basically yes, although you wouldn't need to remove the old object from user defaults first. There can be only 1 item per key, so setting a new object with that same key will inherently remove the old object.

Also, without knowing what your app is doing, I wouldn't create all 3 sets of settings from the get-go. When a user logs in, check to see if there are any settings. If there are, use them. If not, create standard ones and then use those.

Yes, a mutable collection will require more space than a non-mutable one. That's sort of the nature of the beast. But if you need to add, remove, or replace, you really don't have much of a choice.

If you have something that will never change over the life of the app, then sure, set it up as immutable. But otherwise, I wouldn't sweat it too much. It's one of those "prove that you have a problem before you solve it" kind of things. By the time that you have either 1) so many objects or 2) objects so large (like images) that memory is an issue, chances are the memory difference between immutable and mutable collections will be insignificant by comparison. And chances are that if it is THAT much of an issue, you should use plain C arrays instead of NSArrays.

Oh, one thing to keep in mind that is a significant difference between plain dictionaries and NSUserDefaults: primitive types. You can put an integer into user defaults, but not into a dictionary. Same for booleans. In each of these cases, you will need to use NSNumber for the dictionary (this is likely what NSUserDefaults is doing behind the scenes).
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

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

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 01-29-2011, 10:28 PM   #12 (permalink)
Just Some Guy
 
Join Date: Dec 2010
Posts: 112
UnretroGamer is on a distinguished road
Default

Yu know I'm considering going through the hassle of using Facebook Connect to store profile key on the Facebook account so every time you login it downloads your profile key. That way there is only one key, and for other advantages that come with it too of course.
UnretroGamer is offline   Reply With Quote
Reply

Bookmarks

Tags
keys, nsuserdefaults, remove, save, standarduserdefaults

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: 352
15 members and 337 guests
7twenty7, blasterbr, Clouds, dre, EvilElf, iAppDeveloper, jeroenkeij, jimmyon122, Mah6447, Morrisone, n00b, pungs, Sami Gh, stanny, toon4413
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,667
Threads: 94,121
Posts: 402,900
Top Poster: BrianSlick (7,990)
Welcome to our newest member, host number one
Powered by vBadvanced CMPS v3.1.0

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