If you find your self writing code to create a property list file and manipulate its dictionary entries, every time you create a new iOS project, then you should download my PlistManager class file:

PlistManager.swift

In this tutorial you learn how to use the PlistManager class. At the end of the tutorial, you will find a link to an iOS Split View application called Yummy Foods. The PlistManager class is used in the application to perform these functions:

  • Create a plist file
  • Insert food objects in the plist file
  • Fetch all food objects from the plist file and display them in a table view cells
  • Delete a single food object from the plist file
  • Update a food object’s properties in the plist file
  • Delete all food objects from the plist file

How To Use the PlistManager Class

The PlistManager.swift class file has a single property and five functions.

yummy-foods-fig1

Figure 1

To use the PlistManager class in your iOS project you’ll have to create an object from the class like this:

let plistManager = PlistManager()

Once you’ve done that, you call the object’s functions to do things like create a plist file in your application sandbox’s Documents directory, fetch the dictionary from the plist file, etc.

Here is a description of what each function does and how to use it.

getDocsDir()

This function fetch and return the full path to the application sandbox’s Documents directory. The function takes no parameters and returns a Swift string to its caller.

Example of Use

println("Documents Dir Path:\n\(plistManager.getDocsDir())\n")

getPlistFilePath()

This function fetch and return the full path to a plist file that’s in the sandbox’s Documents directory. The function takes a single parameter-the name of the plist file that may or may not exists in the sandbox’s Documents directory. If the plist file doesn’t exists in the sandbox’s Documents directory, the function returns an empty Swift string to its caller.

Example of Use

let plistFilePath = plistManager.getPlistFilePath("Foods.plist")
println("Plist file path:\n\(plistFilePath)\n")

createPlistFile()

This function create a plist file in the sandbox’s Documents directory and initialize the plist file’s dictionary with a default entry. The function takes a single parameter-a filename and returns nothing to its caller. You should never modify or delete the default entry. If you do, the Yummy Foods application will crash.

Example of Use

plistManager.createPlistFile("Foods.plist")

fetchData()

This function fetch a dictionary that’s in the plist file and return it to its caller. The function takes a single parameter-the plist file name and returns an Objective-C NSDictionary to its caller.

Example of Use

var plistData = NSMutableDictionary()
plistData = plistManager.fetchData("Foods.plist") as NSMutableDictionary

saveData()

This function overwrites the existing Dictionary that’s in the plist file. The function takes a single parameter-the plist file name and returns nothing to its caller.

Example of Use

// Create a food object and initialize its properties
var food = Food()
food.name = foodName.text
food.emoji = foodEmoji.text

// Add the food object in the plistData dictionary
plistData.setObject(food.emoji, forKey: food.name)

// Save the plistData dictionary in the plist file
plistManager.saveData("Foods.plist", foodObject: plistRecords)

The Yummy Foods Application

Here is the link to download the Split View application that use the PlistManager class.

Yummy Foods

The application has three views and images below shows what they look like.

yummy-foods-fig2

Figure 2

yummy-foods-fig3

Figure 3

There are two ways to delete food objects in the plist file. By swiping the Master View’s table view cell or clicking the navigation bar’s Delete All button.

Figure 4

Figure 4

There are lots of comments in the application’s class files. You should read them; because they help you understand the source code.

Now, when you run the Yummy Foods app for the first time on the simulator or your real device, code in the AppDelegate.swift file’s didFinishLaunchingWithOptions() creates a plist file called Foods.plist, and insert a default food object in it. Figure 5 shows what the plist file looks like in Xcode’s Property List editor.

Code in the MasterViewController.swift file’s loadAndSortFoodList() function prevent the app from displaying the default dictionary entry in the Master View’s table view cells. Further more, you should never delete or modify that entry. Doing so will crash the Yummy Foods application.

yummy-foods-fig4

Figure 5

That’s all I have to say about the application. Enjoy it and comments are welcomed! 🙂

Tags:

No responses yet

Leave a Reply

UIDocument Demystified: A Step-by-step Guide on Local and iCloud Document Storage

 

Archives