Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.
NSUserDefaults: How to know if a value does not exist?
rocotilosPosts: 3,216iPhone Dev SDK Supporter, Registered Users
I am saving some user settings using NSUserDefaults.
in viewdidload, I retrieve the settings, and apply. Now, if it is a first installation, then this setting does not exist. So, how to check if a setting exist or not?
I am using this code
int FCOLOR = [[NSUderDefaults standardUserDefaults] integerForKey:@\"MyColor\"];
Is there a way to detect if "MyColor" exist or not? Or I just have to manually check for the value (valid or nonvalid)?
KalimbaPosts: 2,181Super Moderators, iPhone Dev SDK Supporter
You should double check the class reference, but I believe there is a "objectForKey" method that will return nil if the key has not yet been assigned. Check this before you do the "integerForKey", as this latter method will return 0 if the key does not exist, and that's not necessarily enough to know that the key doesn't exist.
Is there a way to detect if "MyColor" exist or not? Or I just have to manually check for the value (valid or nonvalid)?
Ta. Cheers.
This question actually suggests a mistake that I've been making until I discovered how NSUserDefaults really work. I suspect you're asking this question because you want to set a default value for MyColor if it does not exist, right?
That's unnecessary extra work, because you can set default values that take effect if there is no user override. This way you can always just use the interForKey and you are guaranteed to always get a result. See my blog post here for an explanation: Defaults for the Defaults @ Cocoanetics
How you would do it is to add a dictionary of default values to your app and load these into the default defaults before accessing any values.
regards
Oliver Drobnik
Cocoanetics - Our DNA is programmed in Objective-C.
Replies
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome10 Detailed Steps to Submit Apps To AppStore
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeThat's unnecessary extra work, because you can set default values that take effect if there is no user override. This way you can always just use the interForKey and you are guaranteed to always get a result. See my blog post here for an explanation: Defaults for the Defaults @ Cocoanetics
How you would do it is to add a dictionary of default values to your app and load these into the default defaults before accessing any values.
Oliver Drobnik
Cocoanetics - Our DNA is programmed in Objective-C.
Linguan makes
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome10 Detailed Steps to Submit Apps To AppStore
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome