PLIST

PLIST's allow you to read and write data to and from a file with an XML layout.

Reading A PLIST From The Resources Folder

First of all make sure you have put the PLIST file in the resources folder in a similar way as you would with images and audio files and add it to your project.

__String *filename = __String::create( "Level.plist");
__Dictionary *plistPattern = __Dictionary::createWithContentsOfFile( filename->getCString( ) );

log( "Int Value: %i", plistPattern->valueForKey( "KeyNameInt" )->intValue( ) );
log( "Float Value: %f", plistPattern->valueForKey( "KeyNameFloat" )->floatValue( ) );
log( "String Value: %s", plistPattern->valueForKey( "KeyNameString" )->getCString( ) );
log( "Bool True Value: %d", plistPattern->valueForKey( "KeyNameBoolTrue" )->boolValue( ) );
log( "Bool False Value: %d", plistPattern->valueForKey( "KeyNameBoolFalse" )->boolValue( ) );
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>KeyNameInt</key>
	<integer>10</integer>
	<key>KeyNameFloat</key>
	<real>20.45</real>
	<key>KeyNameString</key>
	<string>Hello World!</string>
	<key>KeyNameBoolTrue</key>
	<true/>
	<key>KeyNameBoolFalse</key>
	<false/>
</dict>
</plist>

Writing To A PLIST On The Device

__Dictionary *myDictionary = __Dictionary::create( );

myDictionary->setObject( __Integer::create( 4 ), __String::create( "KeyNameInt" )->getCString( ) );
myDictionary->setObject( __Float::create( 5.67 ), __String::create( "KeyNameFloat" )->getCString( ) );
myDictionary->setObject( __String::create( "Hello World!" ), __String::create( "KeyNameString" )->getCString( ) );
myDictionary->setObject( __Bool::create( true ), __String::create( "KeyNameBoolTrue" )->getCString( ) );

__String *fileName = __String::create( "Filename.plist" );
__String *filepath = __String::createWithFormat( "%s%s", FileUtils::getInstance( )->getWritablePath( ).c_str( ), fileName->getCString( ) );

// Save your dictionary to a file
myDictionary->writeToFile( filepath->getCString( ) );

Reading A PLIST That Has Been Saved On A Device

This and the previous section go hand in hand.

__String *fileName = __String::create( "Filename.plist" );
__String *filepath = __String::createWithFormat( "%s%s", FileUtils::getInstance( )->getWritablePath( ).c_str( ), fileName->getCString( ) );

__Dictionary *plistPattern = __Dictionary::createWithContentsOfFile( filepath->getCString( ) );

log( "Int Value: %i", plistPattern->valueForKey( "KeyNameInt" )->intValue( ) );
log( "Float Value: %f", plistPattern->valueForKey( "KeyNameFloat" )->floatValue( ) );
log( "String Value: %s", plistPattern->valueForKey( "KeyNameString" )->getCString( ) );
log( "Bool True Value: %d", plistPattern->valueForKey( "KeyNameBoolTrue" )->boolValue( ) );
log( "Bool False Value: %d", plistPattern->valueForKey( "KeyNameBoolFalse" )->boolValue( ) );