[swift-corelibs-dev] Why is Array.init(contentsOfURL:) not available?

Ole Begemann ole at oleb.net
Mon Dec 5 07:11:19 CST 2016


(Apologies if this is not the right list to ask this question. I couldn't decide between swift-corelibs-dev and swift-dev.)

I noticed that the NSArray and NSDictionary initializers to create a collection from a property list file are not exposed in the stdlib Array and Dictionary types. The same is true for the corresponding methods to write a collection to a property list:

– init?(contentsOfFile:)
– init?(contentsOfURL:)
– write(toFile:atomically:)
– write(to:atomically:)

You can still call these from Swift by using NSArray or NSDictionary directly.

My question is why this decision was made, purely out of interest and because I couldn't find any information about this. I can imagine a few reasons:

1) Property lists are quite specific to Apple platforms and we don't want to "pollute" the standard library with platform-specific solutions.

2) Plists are just one of several potentially useful file formats and users might assume that an initializer to deserialize a collection from a file also supported other file formats, such as JSON or YAML, especially with a generic name like init(contentsOfFile:).

3) The resulting collections are heterogeneously typed ([Any] or [String:Any]) and as such not very pleasant to work with in Swift, so Array and Dictionary are really considered to be the wrong types to offer this functionality. It would be preferable to have a separate data type for working with plists, something like:

enum PropertyList {
    case text(String)
    case bool(Bool)
    case int(Int)
    case double(Double)
    case date(Date)
    indirect case array([PropertyList])
    indirect case dict([String: PropertyList])
}

Thanks!
Ole



More information about the swift-corelibs-dev mailing list