代码之家  ›  专栏  ›  技术社区  ›  Septih

NSURL fileURLWithPath其中NSString有空格

  •  0
  • Septih  · 技术社区  · 14 年前

    我已经看了很多相关的问题,但找不到类似的问题或解决方案,所以如果有重复的地方,我道歉。

    无论如何,我正试图生成一个文件的NSURL以用于NSXMLDocument。我有以下组件:

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

    然后像这样构造URL:

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


    file://localhost/Users/timothyborrowdale/SP2B/Localizable.strings文件

    我试图将项目目录更改为

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

    同样的问题。我还试着完整地键入文件url并使用 [NSURL URLWithString:]

    stringByAddingPercentEscapesUsingEncoding 对于nsutf8编码和nsasciencoding,它们都有相同的问题。

    NSString在传递给NSURL或 但一旦从其中一个输出就有问题了。

    2 回复  |  直到 14 年前
        1
  •  1
  •   Jeremy W. Sherman    14 年前

    试试这个:

    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有许多与平台无关、与路径相关的方法。与硬编码路径扩展分隔符(通常为“.”)和路径组件分隔符(通常为“/”或“\”)相比,更喜欢使用这些分隔符。

        2
  •  0
  •   Peter Hosey    14 年前

    试着放弃 stringWithFormat: (将路径缝合在一起永远不是正确的答案)和 stringByExpandingTildeInPath NSHomeDirectory() stringByAppendingPathComponent: 相反。

    你是怎么得出这个结论的?