Saving a Preference

Because preferences are stored as key/value pairs, you first need to define a key describing the preference value. The key can be anything but must be of type CFString. For example, you might have a key called defaultWindowWidth that defines the width in pixels of any new windows that your application creates.

Next you must decide on an appropriate data type for the value associated with your key. This value can be any of the Core Foundation Property List types, including the container types. If your key is defaultWindowWidth you probably would make its value a CFNumber. You might also decide to combine window width and height into a single preference called defaultWindowSize and make its value be a CFArray containing two CFNumbers.

The code in Listing 1-1 demonstrates how to save a simple preference for an application. The example sets the default text color for the application to blue.

Listing 1-1 Saving a preference value
CFStringRef textColorKey = CFSTR("defaultTextColor"); CFStringRef colorBLUE = CFSTR("BLUE"); // Set up the preference. CFPreferencesSetAppValue(textColorKey, colorBLUE, kCFPreferencesCurrentApplication);

© 2000 Apple Computer, Inc. (Last Updated 14 July 2000)