Dance with NSUserDefaults

Source: Internet
Author: User

In iOS apps, nsuserults ults is very convenient. Programmers can store application options in them, and back up them through iTunes -- they can be displayed in the system setup program. To achieve this, you need to create a Settings. bundle and describe the options you want to display in the setup program and how to display them. Let's demonstrate how to add a Bool value bounces to the setup program. First, create Settings. bundle (using Xcode's New File à Resources à SettingsBundle template ). Edit the Root. plist file:

<? Xml version = "1.0" encoding = "UTF-8"?>
<! DOCTYPE plist PUBLIC "-// Apple // DTD PLIST1.0 // EN"
Http://www.apple.com/DTDs/PropertyList-1.0.dtd>
<Plistversion = "1.0" type = "parmname" text = "parmname">
<Dict>
<Key> StringsTable </key>
<String> Root </string>
<Key> PreferenceSpecifiers </key>
<Array>
<Dict>
<Key> Type </key>
<String> PSToggleSwitchSpecifier </string>
<Key> Title </key>
<String> Bounces </string>
<Key> Key </key>
<String> bounces </string>
<Key> DefaultValue </key>
<True/>
</Dict>
</Array>
</Dict>
</Plist>

Note that we set the default value to true. Next, create a helper class for the Bounces attribute (used to access this attribute ). The bounces attribute is a singleton and is implemented as follows:

-(BOOL) bounces {
Return [[NSUserDefaultsstandardUserDefaults] boolForKey: @ "bounces"];
}

-(Void) setBounces :( BOOL) bounces {
[[NSUserDefaultsstandardUserDefaults] setBool: bounces forKey: @ "bounces"];
[[NSUserDefaultsstandardUserDefaults] synchronize];
}

 

You may know these things, but there are two bugs in this Code.

 

1. If the default value exists, you must specify it in Settings. bundle/Root. plist and register the default value in the code.

 

That is to say, when you use NSUserDefaults to read the attribute (at least for the first time), the default setting in Root. plist is invalid. I don't know if this is already achieved, but I did find this in the test.

To solve this Bug, add the following methods to your Helpler class:

+ (Void) initialize {
[[NSUserDefaultsstandardUserDefaults] registerDefaults:
[NSDictionarydictionaryWithObjectsAndKeys:
[NSNumber numberWithBool: YES], @ "bounces", nil];
}

2. When the application enters the background, synchronize is required.

I was puzzled when I found this point. When your app is transferred to the background, open the system setup program, change the property value, and switch back to the app, you cannot see the new property value! To solve this Bug, you need to explicitly call the synchronize method:

-(Void) applicationWillEnterForeground :( UIApplication *) application {
[[NSUserDefaultsstandardUserDefaults] synchronize];
//...
}

This problem won't attract users' attention, but a high-quality application should pay attention to this. I again stated that the content of this article is not based on any document, but just a problem I encountered in my own tests.

 

From kmyhy's column

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.