Plist v Singleton to access constant data?

What is the practical difference, and or advantages, between using a Plist over a Singleton (or vice versa) to retrieve constant data ?

1 Like

There’s probably a lot more, but here’s some things that immediately sprung to mind:

  • Property lists don’t need to be compiled.

  • Property lists can’t be compiled. This is annoying if, for example, you have minor differences between platforms.

  • Property lists are dynamically typed.

  • Property lists need code to parse and read the values.

  • Property lists can be modified without modifying the code (this is really useful if you give the product to someone who isn’t a developer so that they can tweak the constants).

  • Property list values take up writable memory. Code is always read only, and thus friendlier to the VM system.

I’ll let you decide which are pros and which are cons (-:

Share and Enjoy

Quinn “The Eskimo!” @ DTS @ Apple

4 Likes

I'm unsure what your third point means?

I read that Swift is a Static Type language, designed as such to stamp out C, from whence it came.

I don't understand why. But I read that "swizzle methods" (whatever they are) can still be had from Cocoa, so does the same apply from Plists ?

Property lists come from the Objective-C world and so do not include type information beyond the types declared in the overall schema. For instance, if you have a String backed enum in Swift, property lists can only store the String, not the type it's supposed to be decoded into (barring use of something like NSSecureCoding), and so it's up to you to provide the decoded type.

So Swift is still Static, even if Plists want to cause trouble. If there is no code to interpret it, then it matters not ?

I'm unsure what your third point means?

Imagine you have a UI with multiple columns and you want a property to configure the minimum number of columns. You want an API like this:

struct Configuration {
    static var shared: Configuration { get }
    var minColumnCount: Int { get }
}

which you’d use like this:

for column in 0..<Configuration.shared.minColumnCount {
    … do something …
}

A code implementation might look like this:

struct Configuration {
    static let shared = Configuration()
    let minColumnCount = 3
}

A property list implementation might look like this:

struct Configuration {
    static let shared = Configuration()

    private init() {
        let plistData: Data = … read from a file …
        let root = try! PropertyListSerialization.propertyList(from: plistData, options: [], format: nil)
        let rootDict = root as! [String: Any]
        self.minColumnCount = rootDict["minColumnCount"]! as! Int
        assert(self.minColumnCount > 0)
    }

    let minColumnCount: Int
}

All those exclamation marks and asserts are places where you’re checking types because the underlying data structure (the property list) is dynamically typed.

Note You can do this with less code using Codable but that just means that the compiler is writing all the code to do this checks for you.

Share and Enjoy

Quinn “The Eskimo!” @ DTS @ Apple