Saving App Data in Property List Files


Saving App Data in Property List Files

Your app needs to save data. There's too much data to use User Defaults. Using SwiftData or Core Data would be overkill. One solution is to save the data in property list files. This article shows you how to save your app's data in property list files.

Use the PropertyListEncoder Class if you can

If the data you want to save conforms to the Codable protocol, use the PropertyListEncoder class to save the data. Encoding data with PropertyListEncoder requires you to do the following:

  • Create an instance of PropertyListEncoder.
  • Set the output format: XML or binary.
  • Call the encode function to encode the data to a Data object.

The following code encodes an object to an XML property list:

let encoder = PropertyListEncoder()
encoder.outputFormat = .xml
let data = try encoder.encode(object)

Use the PropertyListDecoder Class to Decode

Normally when your app encodes data, it also needs to decode the data. Use the PropertyListDecoder class to decode the data. Create an instance of PropertyListDecoder and call the decode function to decode the data.

The following code decodes an object from an XML property list:

let decoder = PropertyListDecoder()
object = try decoder.decode(YourClass.self, from: data, format: .xml)

Replace YourClass with the name of your struct or class.

Use NSKeyedArchiver if you can't use PropertyListEncoder

If the data you want to save doesn't conform to Codable, use the NSKeyedArchiver class to save the data to a property list. The easiest way to save data with NSKeyedArchiver is to call the class method archivedData. Supply the object you want to save and specify if you require secure coding. Usually you should require secure coding.

let data = try NSKeyedArchiver.archivedData(withRootObject: object, requiringSecureCoding: true)

Encoding to XML

The default property list format is binary so using the archivedData function saves the data as a binary property list. If you want to save as an XML property list, you must do the following:

  • Create an instance of NSKeyedArchiver.
  • Set the instance's outputFormat property to XML.
  • Call the encode function to encode the data.
  • Call the finishEncoding function when you are done.

The encodedData property has the encoded data. The following function archives data to an XML property list:

func archive(format: PropertyListSerialization.PropertyListFormat = .xml) -> Data {
  let archiver = NSKeyedArchiver(requiringSecureCoding: true)
  archiver.outputFormat = format
  archiver.encode(object, forKey: "root")
  archiver.finishEncoding()
  return archiver.encodedData
}

Use NSKeyedUnarchiver to Unarchive

Use the NSKeyedUnarchiver class to unarchive the data you archived. If you use the archivedData function to archive, call the class function unarchivedObject to unarchive. Supply the class for the data and the data.

object = try NSKeyedUnarchiver.unarchivedObject(ofClass: YourClass.self, 
  from data)

If you are unarchiving an XML property list, you must do the following:

  • Create an instance of NSKeyedUnarchiver.
  • Call the decodeObject function to decode the object.
  • Call the finishDecoding function when you are finished.

The following code unarchives the object that the archive function archived earlier in the article:

let unarchiver = NSKeyedUnarchiver(forReadingFrom: data)
object = unarchiver.decodeObject(key: "root")
archiver.finishDecoding()

Newsletter Archive

You can read previous issues of the newsletter using the following button:

Swift Dev Journal

Subscribe and get exclusive articles on Swift development, a free guide on moving from tutorials to making your first app, notices of sales on books, and anything I decide to add in the future.

Read more from Swift Dev Journal

If you find your app becomes unresponsive at times, it can be frustrating to find the cause. Instruments includes a Hangs instrument that reports hangs to help you find and fix hangs. This article shows you how to use the Hangs and Time Profiler instruments to find hangs and find the code causing the hangs. Profiling Your App In Xcode press Cmd-I to build and profile your app with Instruments. When Instruments launches, it will ask you to choose a template for profiling. Select the Time...

Working with Lists in Multiplatform SwiftUI Apps One of SwiftUI's best features is you can use it to make apps that run on both iOS and Mac. Almost every SwiftUI article you find online is about iOS development, but most of the material also applies to Mac as well. Lists are one area of SwiftUI where there are large differences between iOS and Mac. If you read an article about lists and try to use the code in a Mac app, you'll run into problems. This article provides guidance on writing list...

Getting Started with Document-based SwiftUI Apps Document-based apps let people create documents they can share with others. Examples of document-based apps are text editors, spreadsheets and video editors. Learn the basics of making document-based SwiftUI apps in this article. Creating a Project in Xcode The iOS, Mac, and Multiplatform project categories have a Document App project template. Select that template and click the Next button to create a document app. Multiplatform document apps...