Saturday, July 27, 2019

Introduction to a property list with Swift

What is a property list?


A property list is a file system with the extension plist, which represents an object graph.


When to use it?


It's portable and efficient to store a small amount of data. It should not be used to store a complex object graph. If the data is not likely to change, should we hard-code (store the data with the source code) it instead? For best practices, data should be stored separately from the source code because it's less error prone and we don't need to recompile the program every time when we change the data.


Reading a property list into an array (of a dictionary)


    private var _decimalPlaces:[DecimalPlace] = []
    var decimalPlaces:[DecimalPlace] {
        get {
            if _decimalPlaces.isEmpty {
                do {
                    let dp = try loadAsArray(filename: "DecimalPlaces", format: .xml)
                    _decimalPlaces = toDecimalPlaces(dp)
                } catch {
                    fatalError("Failed to load decimal places from property list file.")
                }
            }
            return _decimalPlaces
        }
    }
    
    func loadAsArray(filename: String, format: PropertyListSerialization.PropertyListFormat = .xml) throws -> [AnyObject] {
        var result:[AnyObject] = []
        if let path = Bundle.main.path(forResource: filename, ofType: "plist") {
            if let xml = FileManager.default.contents(atPath: path) {
                var plistFormat = format
                result = try PropertyListSerialization.propertyList(from: xml, options: .mutableContainersAndLeaves, format: &plistFormat) as! [AnyObject]
            }
        }
        return result
    }
    
    private func toDecimalPlaces(_ src:[AnyObject]) -> [DecimalPlace] {
        var result:[DecimalPlace] = []
        for i in 0..

The source code above works with the property list file, DecimalPlaces.plist, with the contents below. It's in the root directory of the project like Info.plist file.


Below is the content of DecimalPlace class:
    
class DecimalPlace {
    var id:Int!
    var value:UInt?
    var label:String!
    var description:String!
    
    init() {       
    }
}
    


Reading a property list into a dictionary


    func load(filename: String, format: PropertyListSerialization.PropertyListFormat = .xml) throws -> [String:AnyObject] {
        var result:[String:AnyObject] = [:]
        if let path = Bundle.main.path(forResource: filename, ofType: "plist") {
            if let xml = FileManager.default.contents(atPath: path) {
                var plistFormat = format
                result = try PropertyListSerialization.propertyList(from: xml, options: .mutableContainersAndLeaves, format: &plistFormat) as! [String:AnyObject]
            }
        }
        return result
    }
    

The code snippets above works with the property list file with the contents below:






References
https://developer.apple.com/library/archive/documentation/General/Conceptual/DevPedia-CocoaCore/PropertyList.html

No comments:

Post a Comment