views:

196

answers:

1

I want to fill a NSMutableDictionary with content out of a URL File. What type file must it be and how must it be structured?

NSMutableDictionary *myMutableDictionary = [[NSMutableDictionary alloc] 
    initWithContentsOfURL:[NSURL URLWithString:@"http://example.com/list.txt"]]; 
+1  A: 

It has to be in the XML property list format 1:

An URL that identifies a resource containing a string representation of a property list whose root object is a dictionary. The dictionary must contain only property list objects (instances of NSData, NSDate, NSNumber, NSString, NSArray, or NSDictionary).

See here for an overview of the property list types.

E.g. the following file:

<plist version="1.0">
  <dict>
    <key>A</key><string>something</string>
    <key>B</key><integer>42</integer>
  </dict>
</plist>

... becomes a dictionary containing:

{
  A = something;
  B = 42;
}
Georg Fritzsche
Also, it can be an old-style dictionary format just containing `"key1" = "value1"; "key2" = "value2"` just like a `Localizable.strings` file.
dreamlax
bbum
@bbum: If Strings files aren't plists, why does it produce this error when not formatted properly: `23/05/10 11:53:52 AM App[74572] CFPropertyListCreateFromXMLData(): Old-style plist parser: missing semicolon in dictionary.` Also, the documentation says that it is a property list representation. If it's so heavily deprecated why is it still being used for strings tables . . .
dreamlax
Implementation detail; the parser for string lists was borrowed from the old-school plist parser, but special cased for string lists.. Plists require that the outermost item is a collection -- a dictionary, IIRC.
bbum
@bbum: "Heavily" deprecated? Where did you read that? While old-style plist is [supported for legacy reason](http://developer.apple.com/mac/library/documentation/cocoa/conceptual/propertylists/OldStylePlists/OldStylePLists.html), there's no sign it will go away soon. And since when "strings is a plist" is an implementation detail? It's [clearly documented](http://developer.apple.com/iphone/library/documentation/MacOSX/Conceptual/BPInternational/Articles/StringsFiles.html).
KennyTM
I don't know about the deprecation part, but the documentation only officially mentions plists being supported for `NSDictionary`. The i18n doc only mentions that a plist format can also be used for strings files, which doesn't imply the reverse.
Georg Fritzsche
From the docs: "*Important:* The `NSPropertyListOpenStepFormat` constant is not supported for writing. It can be used only for reading old-style property lists." and "OpenStep format (use of this format is discouraged)."
bbum