0
NSData *sourceData = [[NSData alloc]initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Information" ofType:@"plist"]];
NSString *sourceString = [[NSString alloc]initWithData:sourceData encoding:NSUTF8StringEncoding];

Yields an empty string (and yes, NSData is loaded correctly).

From here: https://stackoverflow.com/a/6412275/555690, it says that I need to use the correct encoding.

When I go to Information.plist and check the source code, it begins with

<?xml version="1.0" encoding="UTF-8"?>

So, as far as I'm concerned, I'm indeed using the right encoding. What is the problem then?


Note

I'm not really trying to read the contents of Information.plist (I know I can do this by doing something like dictionaryWithContentsOfFile etc etc) - I am just curious about converting its corresponding NSData to an NSString.

Community
  • 1
  • 1
Saturn
  • 17,888
  • 49
  • 145
  • 271
  • Could you put up the file on an ftp server or a public dropbox, or something like that? My first suspicion is that there is some sort of corruption in there. – fieldtensor Jun 06 '14 at 06:19
  • You should try other encoding to check if it solves your problem, the tag you are reading when you are opening the file it's just for information, the file can be encoded with other encoding. I'm not sure if this command exists on mac, but try it: from command line type `file -bi yourFile` and it will output the encoding of the file. – gabuh Jun 06 '14 at 06:25
  • You should `NSLog` the `sourceData`, looking at the hexadecimal representation, to make sure it loaded correctly (e.g. it's possible that it's not finding the file; perhaps not included in bundle; perhaps on device and capitalization isn't right; etc.) and that if it is, look at it and see if it's a valid `NSUTF8StringEncoding` (e.g. not a standard plist, but rather something like a `NSKeyedArchiver` archive, which is a bplist, binary plist). Often plists are loaded as `NSDictionary` or `NSArray` objects, anyway, not as `NSString` objects. How was this plist created? – Rob Jun 06 '14 at 06:33
  • I am quite sure if you iterate through each byte of your data, creating a character with it and packing it into string (or simply logging it) you will find out there are invalid characters. So truly not every file can be red by simply converting the data to string, this method is meant to convert raw data to string when those data were in fact made from string, one wrong character and the operation breaks. Even if the file seems to be plain text there might be a binary header in it. – Matic Oblak Jun 06 '14 at 07:06
  • Are you sure it is not a binary plist? Try open it from your bundle in textedit and check if it starts with `bplist`. – 3329 Jun 06 '14 at 17:09

0 Answers0