views:

46

answers:

2

Hello,

I've looked at quite a few of the related questions and cannot find a similar problem or a solution so my apologies if there is a duplicate out there somewhere.

Anyway, I'm trying to generate a file's NSURL to use with an NSXMLDocument. I have the following components:

const NSString * PROJECT_DIR = @"~/SP\\ BB/";
const NSString * STRINGS_FILE = @"Localizable.strings";

and construct the URL like so:

NSURL * stringsURL = [NSURL fileURLWithPath:[[NSString stringWithFormat:@"%@%@",PROJECT_DIR,STRINGS_FILE] stringByExpandingTildeInPath]];

however, the resulting path in the NSURL is:
file://localhost/Users/timothyborrowdale/SP2B/Localizable.strings

I have tried changing the PROJECT_DIR to

@"~/SP BB/"
@"~/SP\\\\ BB/" (changes to SP엀2B)
@"~/SP%20BB/"
@"~/SP\%20BB/"

with the same problem. I also tried typing out the file url completely and using [NSURL URLWithString:]

I have also tried using stringByAddingPercentEscapesUsingEncoding with both NSUTF8Encoding and NSASCCIEncoding and these have the same issue.

The NSString displays properly before being passed to NSURL or stringByAddingPercentEscapesUsingEncoding but has the problem once outputted from either.

A: 

Try abandoning stringWithFormat: (never the right answer for stapling paths together) and stringByExpandingTildeInPath and using NSHomeDirectory() and stringByAppendingPathComponent: instead.

  • @"~/SP\\ BB/" (changes to SP엀2B)

How did you arrive at that conclusion?

Peter Hosey
That hasn't worked either. Again the initial NSString is fine, but not the url. Regarding that line, I just copied the text from the console.
Septih
Septih: What did you print to the Console, and how did you print it?
Peter Hosey
This code is part of a unit test, so I'm hacking around with my STAssertTrue(condition,format,...) lines to see what's going on, so really the line is coming from the build results console when the build fails. The actual item printed is the NSURL description.
Septih
+1  A: 

Try this:

NSString *fnam = [@"Localizable" stringByAppendingPathExtension:@"strings"];
NSArray *parts = [NSArray arrayWithPathComponents:@"~", @"SP BB", fnam, (void *)nil];
NSString *path = [[NSString pathWithComponents:parts] stringByStandardizingPath];
NSURL *furl = [NSURL fileURLWithPath:path];

Foundation has a host of platform-independent, path-related methods. Prefer those over hard-coding path extension separators (often ".") and path component separators (often "/" or "\").

Jeremy W. Sherman
I'm afraid this doesn't work, the path is created fine, but the url has the same problem
Septih
How did you determine that? Does reading in the file using `+[NSString stringWithContentsOfURL:usedEncoding:error:` work? If not, what error does it give?
Jeremy W. Sherman
That works actually. Perhaps it's not the path that's at fault. I'll try getting the err from the XMLDocument init.
Septih