PLIST file in iOS

· 2 min read
PLIST file in iOS

A Property List (PLIST) file is a file format used by macOS and iOS to store serialized data. It is often used to store data such as user preferences, settings, and other information that needs to be stored persistently. PLIST files can either be stored in a binary format and are typically used to store small amounts of data. or they can also be stored in a human-readable XML format.

In iOS, PLIST files are commonly used to store information about the app and its configuration. For example, an app's Info.plist file may contain information about the app's bundle identifier, version number, and other metadata. PLIST files can also be used to store user preferences and other data that needs to be stored persistently by the app.

PLIST files can be edited manually, but it is usually more convenient to use the property list editor built into Xcode or other tools to edit them. It is also possible to read and write PLIST files programmatically using the APIs provided by the iOS SDK.

Info.plist

The Info.plist file contains metadata about the app, such as the app's bundle identifier, version number, and other information.

Example of custom plist

<?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>Name</key>
	<string>John Smith</string>
	<key>Age</key>
	<integer>30</integer>
	<key>Pets</key>
	<array>
		<string>dog</string>
		<string>cat</string>
	</array>
</dict>
</plist>

This PLIST file contains a dictionary with three key-value pairs: "Name", "Age", and "Pets". The "Name" and "Pets" values are strings, while the "Age" value is an integer. The "Pets" value is also an array containing two strings, "dog" and "cat".

This PLIST file could be used to store information about a person, such as their name, age, and pets. It could then be read and used by an app to display this information to the user or to store it persistently.

Read custom plist file in Swift

To read a PLIST file in Swift, you can use the PropertyListDecoder class from the Foundation framework. Here is an example of how you could use this class to read a PLIST file and decode it into a Swift object:

import Foundation

struct Person: Decodable {
    let name: String
    let age: Int
    let pets: [String]
}

let fileURL = URL(fileURLWithPath: "path/to/file.plist")

do {
    let data = try Data(contentsOf: fileURL)
    let decoder = PropertyListDecoder()
    let person = try decoder.decode(Person.self, from: data)
    print(person)
} catch {
    print(error)
}

In this example, the Person struct conforms to the Decodable protocol, which allows it to be decoded from a PLIST file. The PropertyListDecoder is used to decode the data from the file into an instance of the Person struct.

Note that this example assumes that the PLIST file is in the XML format and that its structure matches the structure of the Person struct. If the PLIST file is in the binary format or has a different structure, you may need to use a different method to read and decode the file.