Takuji's iPhone App Development

Just started iPhone app development. This blog is about all kinds of things related to app development.

【Objective-C】How to make .plist files and how to read them

I was thinking about how to read spawn data of enemies from my app, BeeCluster.
It is possible to hard-code the data, but it would be better to read it from an external file, such as .plist files.
So, I googled about .plist files and here's the outcome.

A .plist file can be created from Xcode by "File -> New -> File..." and choose Property List in the Resource category. And you can edit the .plist file from Xcode, but it is very time consuming to input each single line by clicking and typing. So, I had to find another better way and finally found this site. This site will convert your csv file into a .plist file.
http://www.mindsizzlers.com/case-studies/tools/csv-to-plist-online-conversion-tool/
f:id:takujidev:20130421195511p:plain
Just select a csv file and click on Upload csv and you will see the screen like this. Right click "here" and you'll get a .plist made from your cdv file in your download folder or anywhere else you like.
f:id:takujidev:20130421195644p:plain

I tried testing this site using a test file like this. I made this using Libre Office Calc. Of course you can use Excel to make a cdv file.

type,positionX,positionY,interval
fly,-100,550,1.5
fly,100,550,1
ladybugL,-200,300,0
ladybugR,200,300,0

The plist file created is as follows:

<?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">
        <array>
                <dict>
                        <key>type</key>
                        <string>fly</string>
                        <key>positionX</key>
                        <integer>-100</integer>
                        <key>positionY</key>
                        <integer>550</integer>
                        <key>interval</key>
                        <real>1.5</real>
                </dict>
                <dict>
                        <key>type</key>
                        <string>fly</string>
                        <key>positionX</key>
                        <integer>100</integer>
                        <key>positionY</key>
                        <integer>550</integer>
                        <key>interval</key>
                        <integer>1</integer>
                </dict>
                <dict>
                        <key>type</key>
                        <string>ladybugL</string>
                        <key>positionX</key>
                        <integer>-200</integer>
                        <key>positionY</key>
                        <integer>300</integer>
                        <key>interval</key>
                        <integer>0</integer>
                </dict>
                <dict>
                        <key>type</key>
                        <string>ladybugR</string>
                        <key>positionX</key>
                        <integer>200</integer>
                        <key>positionY</key>
                        <integer>300</integer>
                        <key>interval</key>
                        <integer>0</integer>
                </dict>
        </array>
</plist>

And here's how to read this plist file from your Objective-C program. All the date will be read into the "table" array. "pathForResource" method accepts a file name split in two parts. The first parameter is the part before the dot and the second parameter is the part after the dot. For example, testfile.plist, should be @"testfile" for the first parameter, and @"plist" for the second parameter.

NSBundle* bundle = [NSBundle mainBundle];
NSString* path = [bundle pathForResource:@"testfile" ofType:@"plist"];
NSArray* table = [NSArray arrayWithContentsOfFile:path];

To get a line of spawn data from the "table" array, use "objectAtIndex" method and the data will be stored in a Dictonary. And each single part of the line from the dictionary data, use "objectForKey" method. For non string type data, you'll need to convert it to, for example, float data by "floatValue" method.

    NSDictionary* element = [table objectAtIndex:index];
    NSString* type = [element objectForKey:@"type"];
    float positionX = [[element objectForKey:@"positionX"] floatValue];
    float positionY = [[element objectForKey:@"positionY"] floatValue];
    float interval = [[element objectForKey:@"interval"] floatValue];