Search code examples
objective-cmacoscocoaosx-yosemitensurl

NSURL fileUrlWithPath method returns double path


this is the scenario: In a sandboxed app for OS X 10.10.5 I have some path saved in NSString object, say @"file:///Users/xxx/".

Then I execute [NSURL fileURLWithPath:object]. That gives me NSURL object like this

@"file:/Users/xxx -- file:///Users/xxx/Library/Containers/com.123456.App/Data/"

.

I only need this part @"file:///Users/xxx/Library/Containers/com.123456.App/Data/"

Somehow the source string is twisted and doubled and extra dashes added in the middle. Can anyone explain why does it happen?

Xcode 6.4


Solution

  • fileURLWithPath: will return a file URL path.

    i.e starting with: file:///

    This means that the string path that you pass it should be in the form of:

    @"/Users/xxx/Library/Containers/com.123456.App/Data/"
    

    You do not need to prepend the path with file:///. Or you will get the result that you are getting.


    Example:

    NSString * stringPath = @"/Users/xxx/Library/Containers/com.123456.App/Data/";
    
        NSURL * anUrl =[NSURL fileURLWithPath:stringPath ];
    
    
         NSLog(@"nUrl %@",anUrl);
    

    ----> nUrl file:///Users/xxx/Library/Containers/com.123456.App/Data/