Using the PLIST file
(Property List file)
So I thought I’d go into more detail for one way to handle data in your app – the plist file.
It’s been around for awhile – Apple uses it for many software programs on your computer. It’s basically a property list file in XML format.
So let’s start with a basic file. It looks like this:
If you’ve ever seen html code, this looks very similar to it – yes??
there’s a <key> and data such as <string> or <integer>
So how do you get this file?
Creating the plist file
This file can be created in a few ways:
- Create it directly in your app (in Xcode)
- Create it using an html editor
For creating in your app
- Open your project
- Choose File>New >File and then Resource, Property list
- Save in your project directory and then you can start adding data.
CREATING PLIST outside of your app
Here’s how I created one outside of Xcode:
In my latest project I wanted to access a company roster. I already had the data in an excel file. So for this project, I started by creating a Word “merge” file that added the <key> and <string> tags around the data.
Once I merged the excel data into a new Word document, I copied the entire Word file into TextEdit (free program on macs). I saved it as “roster.plist” (you can use any name you want “anyFile.plist”)
You can use any html editor besides TextEdit, such as HTML-Kit (free) or Dreamweaver. You want to use an HTML editor because Word adds extra characters to your file. It looks okay in Word, but doesn’t act right when you pull it into your project.
Once you have your plist file created, import it into your Xcode project and you’re off to the races. How do you import? You import by either dragging it into your project from Finder (mac) or go to your menu in Xcode
File > Add Files to (project name) >
then go to the directory where you created the file and choose it.
One more thing to note, even though you created the file outside of Xcode, you can always edit it in Xcode.
You can find the plist file in your project. When you click on it, it will display the data. The file is highlighted on the left (in the menu directory) and the data is on right.
You can look at the data as source code or as a property list. How? Just right click on it (that’s control click for mac users). You’ll get a menu – choose “Open as” and you’ll see the two choices
It’s easier to learn with videos than reading I think, so here’s some good ones to get you started (note – these projects are all in Objective-C, not Swift) BUT it works the same way.
Example One – PLIST
This is a very simple video (but no voice). They start with creating the file (like I explained above), and then added data from there. They provide a link to download the source code.
Example Two – PLIST
In this video, things are explained better and it has audio which is super helpful (how to access file, how to write and read data using code).
And one last thing – you can always just edit the data using Xcode software which adds the keys for you. You don’t have to do it in code.