<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Hi Ole,<div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Dec 5, 2016, at 5:11 AM, Ole Begemann via swift-corelibs-dev <<a href="mailto:swift-corelibs-dev@swift.org" class="">swift-corelibs-dev@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div class="">(Apologies if this is not the right list to ask this question. I couldn't decide between swift-corelibs-dev and swift-dev.)<br class=""><br class="">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:<br class=""><br class="">– init?(contentsOfFile:)<br class="">– init?(contentsOfURL:)<br class="">– write(toFile:atomically:)<br class="">– write(to:atomically:)<br class=""><br class="">You can still call these from Swift by using NSArray or NSDictionary directly.<br class=""><br class="">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:<br class=""><br class="">1) Property lists are quite specific to Apple platforms and we don't want to "pollute" the standard library with platform-specific solutions.<br class=""><br class="">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:).<br class=""><br class="">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:<br class=""><br class="">enum PropertyList {<br class=""> case text(String)<br class=""> case bool(Bool)<br class=""> case int(Int)<br class=""> case double(Double)<br class=""> case date(Date)<br class=""> indirect case array([PropertyList])<br class=""> indirect case dict([String: PropertyList])<br class="">}<br class=""><br class="">Thanks!<br class="">Ole<br class=""><br class=""></div></div></blockquote><br class=""></div><div>The main reason is actually that we just prefer people use NSPropertyListSerialization API to read or write property lists. One big reason: that API has the possibility of giving you an error, whereas the collection methods do not.</div><div><br class=""></div><div>The collection API is a trivial convenience method over property list serialization anyway. Here is the implementation:</div><div><br class=""></div><div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> data = [[</span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">NSData</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #3e1e81" class="">allocWithZone</span><span style="font-variant-ligatures: no-common-ligatures" class="">:</span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">NULL</span><span style="font-variant-ligatures: no-common-ligatures" class="">] </span><span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">initWithContentsOfFile</span><span style="font-variant-ligatures: no-common-ligatures" class="">:path];</span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">if</span><span style="font-variant-ligatures: no-common-ligatures" class=""> (!data) </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">return</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">nil</span><span style="font-variant-ligatures: no-common-ligatures" class="">;</span></div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; color: rgb(49, 89, 93);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">id</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> plist = [[</span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">NSPropertyListSerialization</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures" class="">propertyListWithData</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">:data </span><span style="font-variant-ligatures: no-common-ligatures" class="">options</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">:immutable ? </span><span style="font-variant-ligatures: no-common-ligatures" class="">NSPropertyListImmutable</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> : </span><span style="font-variant-ligatures: no-common-ligatures" class="">NSPropertyListMutableContainers</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures" class="">format</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">:</span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">NULL</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures" class="">error</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">:</span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">NULL</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">] </span><span style="font-variant-ligatures: no-common-ligatures; color: #3e1e81" class="">retain</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">];</span></div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> [data </span><span style="font-variant-ligatures: no-common-ligatures; color: #3e1e81" class="">release</span><span style="font-variant-ligatures: no-common-ligatures" class="">];</span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo;" class=""><br class=""></div></span></div></span></div></div><div>Plus an additional check to make sure the result is the right type (array or dictionary).</div><div><br class=""></div><div>- Tony</div><div><br class=""></div><br class=""></div></body></html>