iosnsstringplistruntime-errorinitwithcontentsoffile

Getting Error Domain=NSCocoaErrorDomain Code=261 with simple case


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?


Solution

  • 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.