How would I save all keys in [NSUserDefaults standardUserDefaults]? To save all settings.
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.
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.
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.
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.
>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.
>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.
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.
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!
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.
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).
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.