views:

462

answers:

1

I have a NSURL to a local file with no base url. I want to change the base URL to another local directory, with the relative path being in respect to that URL. For example

 NSURL *path = [NSURL fileURLWithPath: @"/Users/username/Desktop/Photos/Photo1.jpg"];

Now, say I want to change the base part to /Users/username/Desktop/SomeFolder, then I would want the NSString returned by [path relativePath]; to be "../Photos/Photo1.jpg"

Is there an easy way to do this? I can't seem to find any NSURL methods that do this. I could go through the path components of the new base and old absolute paths to construct the relative part my self, but is there an easier way?

+2  A: 

NSURL objects are immutable so there is no way to change the base URL after creating it. Your best bet is creating a new NSURL instance with URLWithString:relativeToURL:. However, I still think what you want will not work. The docs for -[NSURL relativePath] state that if the receiver is an absolute URL (which your example is), this method returns the same value as path.

Ole Begemann