Search code examples
iosnsstringnsurl

NSURL URLWithString is shortening very long strings with an ellipsis "...", creating invalid NSURL objects


I am trying to use NSURL URLWithString to create an NSURL object from a potentially very long file path with a very long file name. When I convert the file path to an NSURL object using URLWithString, the path gets shortened and an ellipsis " ... " is put in the NSURL:

Path as NSString: /var/mobile/Containers/Data/Application/APPLICATION_ID/Documents/TEMP/this_is_a_really_really_and_i_mean_reaeeeeaaaallly_long_file_name/this_is_a_really_really_and_i_mean_reaeeeeaaaallly_long_file_name.xml

// Convert to NSURL using this technique:
[NSURL URLWithString:pathAsString]

Path as NSUrl:    /var/mobile/Containers/Data/Application/APPLICATION_ID/Documents/TEMP/this_is_a_really_really_and_ ... name.xml

I can't find any way around this problem, and I get an error when I try to open the file at the NSURL I'm trying to create, saying that there is no such file or directory:

Error Domain=NSCocoaErrorDomain Code=260 "The operation couldn’t be completed. (Cocoa error 260.)" UserInfo=0x15ec82b0 {NSFilePath=/var/mobile/Containers/Data/Application/APPLICATION_ID/Documents/TEMP/this_is_a_really_really_and_ ... name.xml, NSUnderlyingError=0x15eac4a0 "The operation couldn’t be completed. No such file or directory"}

I don't know why the ellipsis is being added and I can't figure out how to get around this problem.


Solution

  • Your code

    NSString *const path = @"/var/mobile/Containers/Data/Application/APPLICATION_ID/Documents/TEMP/this_is_a_really_really_and_i_mean_reaeeeeaaaallly_long_file_name/this_is_a_really_really_and_i_mean_reaeeeeaaaallly_long_file_name.xml";
    NSURL *const url = [NSURL URLWithString:path];
    NSLog(@"%@", url);
    NSLog(@"%@", url.path);
    

    prints

    /var/mobile/Containers/Data/Application/APPLICATION_ID/Documents/TEMP/this_is_a_really_really_and_i_mean_reaeeeeaaaallly ... name.xml
    /var/mobile/Containers/Data/Application/APPLICATION_ID/Documents/TEMP/this_is_a_really_really_and_i_mean_reaeeeeaaaallly_long_file_name/this_is_a_really_really_and_i_mean_reaeeeeaaaallly_long_file_name.xml
    

    The URL has the path stored correctly, NSURLs description simply shortens it when logging the URL. Your problems appears to lie somewhere else.