initWithContentsOfURL what is the encoding?

428 views Asked by At

Hi I saved a URL that has a path to a directory where I will be storing information, Now I want to retrieve the information but I don't know how to receive the items, I am trying to use initWithContentsOfURL but I dont know what the encoding would be?

This is how I saved the URL

//DirectoryPaths has the NSCAcheDirectory
 dirPath = [[directoryPaths objectAtIndex:0] URLByAppendingPathComponent:[NSString stringWithFormat:@"photos.jpg"]];

How do I get a path to the URL now? I tried

  NSString *pathToFile = [[NSString alloc] initWithContentsOfURL:dirPath
                                                 usedEncoding:nil
                                                        error:&error];

I have no clue what the encoding would be since I didnt use one to store the file?

2

There are 2 answers

0
Tommy On

I think you've misunderstood; initWithContentsOfURL:usedEncoding:error: loads a string from a URL. So you'd specify the encoding used for the string — UTF-8, ASCII or something like that.

To get a disk path from a URL you probably want [dirPath path]. In practice you can probably just load from the URL rather than hardcoding local disk behaviour.

0
Kimpoy On

Use NSUTF8StringEncoding or ASCII. It works for me. :)