< Previous PageNext Page > Hide TOC

Creating Property Lists

The examples in this section demonstrate how to create and work with property lists. The error checking code has been removed for clarity. In practice, it is vital that you check for errors because passing bad parameters into Core Foundation routines can cause your application to crash.

Listing 1 shows you how to create a very simple property list—an array of CFString objects.

Listing 1  Creating a simple property list from an array

#include <CoreFoundation/CoreFoundation.h>
#define kNumFamilyMembers 5
 
void main () {
    CFStringRef names[kNumFamilyMembers];
    CFArrayRef  array;
    CFDataRef   xmlData;
 
    // Define the family members.
    names[0] = CFSTR("Marge");
    names[1] = CFSTR("Homer");
    names[2] = CFSTR("Bart");
    names[3] = CFSTR("Lisa");
    names[4] = CFSTR("Maggie");
 
    // Create a property list using the string array of names.
    array = CFArrayCreate( kCFAllocatorDefault,
                (const void **)names,
                kNumFamilyMembers,
                &kCFTypeArrayCallBacks );
 
    // Convert the plist into XML data.
    xmlData = CFPropertyListCreateXMLData( kCFAllocatorDefault, array );
 
    // Clean up CF types.
    CFRelease( array );
    CFRelease( xmlData );
}

Listing 2 shows how the contents of xmlData, created in Listing 1, would look if printed to the screen.

Listing 2  XML created by the sample program

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN"
        "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
    <string>Marge</string>
    <string>Homer</string>
    <string>Bart</string>
    <string>Lisa</string>
    <string>Maggie</string>
</array>
</plist>
 


< Previous PageNext Page > Hide TOC


© 2006 Apple Computer, Inc. All Rights Reserved. (Last updated: 2006-02-07)


Did this document help you?
Yes: Tell us what works for you.
It’s good, but: Report typos, inaccuracies, and so forth.
It wasn’t helpful: Tell us what would have helped.