4

I have the following bit of code. path is a bundle path to a file in my bundle.

    _dictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
    NSError *error;
    NSString *string = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];

What I don't understand is the _dictionary variable looks fine. It was able to get the contents and convert it to a dictionary. However, the simple case of converting the file to a string is giving me an error:

$2 = 0x12eb3d50 Error Domain=NSCocoaErrorDomain Code=261 "The operation couldn’t be completed. (Cocoa error 261.)" UserInfo=0x12eb3550 {NSFilePath=/Users/ben/Library/Application Support/iPhone Simulator/6.1/Applications/82CBC7A6-2C4C-4330-9B29-7940C961B5B7/FMA.app/Licenses.plist, NSStringEncoding=4}

How can I read the PLIST file as a simple XML string? I've checked the encoding and the file is UTF-8 encoded (as is also indicated by the XML content headers). If I change the encoding to something else, I don't get an error, but the string is garbled! Any ideas?

Kudit
  • 4,212
  • 2
  • 26
  • 32

1 Answers1

1

If Xcode is copying the plist file into your app bundle then you should ensure it is copied as an XML file and not as binary (which seems to be the default setting now).

In Xcode, go to your project, select the target, go to build settings, search for 'property', change the drop-down setting for 'Property List Output Encoding' to XML.

Wain
  • 118,658
  • 15
  • 128
  • 151
  • I tried usedEncoding and got the following error and encoding: `(NSStringEncoding) $3 = 13635557` and `$4 = 0x09277b00 Error Domain=NSCocoaErrorDomain Code=264 "The operation couldn’t be completed. (Cocoa error 264.)" UserInfo=0x92772d0 {NSFilePath=/Users/ben/Library/Application Support/iPhone Simulator/6.1/Applications/82CBC7A6-2C4C-4330-9B29-7940C961B5B7/FMA.app/Licenses.plist}` – Kudit Apr 08 '13 at 20:03
  • In that case, try NSPropertyListSerialization: `+ (id)propertyListWithData:(NSData *)data options:(NSPropertyListReadOptions)opt format:(NSPropertyListFormat *)format error:(NSError **)error` --- is the plist actually in binary format? – Wain Apr 08 '13 at 20:45
  • I can open it in Text Edit, so it's not in binary format. How do I get the string of the file from the NSPropertyListSerialization? The reason I'm doing this is because I want to get the order of the keys in the `NSDictionary` but when I do `[[NSMutableDictionary alloc] initWithContentsOfFile:path]` of course the ordering of the keys is indeterminate so I need to parse the file to see how the keys are ordered in the file. – Kudit Apr 08 '13 at 20:51
  • What significance does the order of the keys have? What are you actually trying to do with the data in the plist? – Wain Apr 08 '13 at 20:54
  • It's a list that maps keys to sets of ids. I'm displaying the keys in a `UITableView` but they need to be ordered the way the client wants (which isn't alphabetical). – Kudit Apr 08 '13 at 20:57
  • Then I'd say the appropriate solution is to store the data differently. Either as 2 different files (data in your dict, order in an array), or an array of dicts (each dict one key (map key) value (ids) pair). The array of dicts is more flexible for the future as your dicts can have other context info added. – Wain Apr 08 '13 at 21:07
  • 1) that seems horribly inefficient and convoluted and 2) this doesn't solve the issue of being able to read in a file into a string. This isn't the only case where I want an ordered dictionary structure as I'm also creating it from a web service which works fine as the items get inserted in order and thus I can track the ordering of the items. I can't update the web-service to return alternate data simply because `initWithContentsOfFile` doesn't work. – Kudit Apr 08 '13 at 21:11
  • We should move to a chat really... When you say you opened the plist in text edit, was this from the file in the simulator directory? Check this answer [link](http://stackoverflow.com/questions/12358502/does-xcode-implicitly-convert-plistss-to-binary-format) - is the file converted to binary when saved into the app – Wain Apr 08 '13 at 21:17
  • Can't move to chat because you only have a reputation of 1. The plist is definitely a text file and is not converted to binary: http://kudit.com/dump/Licenses.plist – Kudit Apr 08 '13 at 21:24
  • Xcode now seems to default to copy as binary. If I try your file copied as a binary (which opens as text in text edit but is truly binary if you use a hex editor) I get an error. If I set the build to copy as XML I don't and the string loads as expected. – Wain Apr 08 '13 at 21:49
  • I bet that's the problem! Where is the setting to copy as XML instead of as binary? – Kudit Apr 08 '13 at 21:52
  • Go to your project, select the target, go to build settings, search for 'property', change the drop-down setting for 'Property List Output Encoding' to XML – Wain Apr 08 '13 at 22:02
  • Awesome! That did it. Could you please post this answer so that I can approve it? – Kudit Apr 08 '13 at 22:03
  • i am just trying to read /Library/Preferences/com.apple.alf.plist but I am getting this error – Vikas Bansal Aug 03 '15 at 10:25