【问题标题】:-[NSString stringByAppendingPathComponent:] or just -[NSString stringByAppendingFormat:] for NSStrings for NSURLs?-[NSString stringByAppendingPathComponent:] 或者只是 -[NSString stringByAppendingFormat:] 用于 NSStrings 用于 NSURL?
【发布时间】:2012-09-06 21:02:55
【问题描述】:

当调用 +[NSURL URLWithString:] 时,我有两个选项来构建我的 URL:

[[@"http://example.com" stringByAppendingPathComponent:@"foo"] stringByAppendingPathComponent:@"bar"]

[@"http://example.com" stringByAppendingFormat:@"/%@/%@",@"foo",@"bar"];

-[NSString stringByAppendingPathComponent:] 似乎是更正确的答案,但是除了在以下情况下处理双斜杠之外,使用-[NSString stringByAppendingFormat:] 是否会丢失任何东西?

// http://example.com/foo/bar
[[@"http://example.com/" stringByAppendingPathComponent:@"/foo"] stringByAppendingPathComponent:@"bar"] 

// http://example.com//foo/bar  oops!
[@"http://example.com/" stringByAppendingFormat:@"/%@/%@",@"foo",@"bar"];

【问题讨论】:

  • stringByAppendingPathComponent 理论上将使用“系统路径分隔符”与硬连接到您的格式中的路径分隔符,使您的代码(稍微更多)与系统无关。当然,Objective-C 在 Windoze 上不是很常用,所以这不是什么大问题。

标签: objective-c foundation


【解决方案1】:

当您使用 URLS 时,您应该使用 NSURL 方法:

NSURL * url = [NSURL URLWithString: @"http://example.com"];
url = [[url URLByAppendingPathComponent:@"foo"] URLByAppendingPathComponent:@"bar"]

或在 Swift 中

var url = NSURL.URLWithString("http://example.com")
url = url.URLByAppendingPathComponent("foo").URLByAppendingPathComponent(".bar")

【讨论】:

    【解决方案2】:

    我刚刚遇到了 stringByAppendingPathComponent 的问题:它到处都删除了双斜线!:

    NSString* string1 = [[self baseURL] stringByAppendingString:partial];
    NSString* string2 =  [[self baseURL] stringByAppendingPathComponent:partial];
    
    NSLog(@"string1 is %s", [string1 UTF8String]);
    NSLog(@"string2 is %s", [string2 UTF8String]);
    

    对于https://blah.com的baseURl

    和/moreblah的一部分

    产生两个字符串:

    2012-09-07 14:02:09.724 myapp string1 是 https://blah.com/moreblah

    2012-09-07 14:02:09.749 myapp string2 是 https://blah.com/moreblah

    但由于某种原因,我致电 blah.com 以使用单斜杠获取资源。但它向我表明 stringByAppendingPathComponent 用于路径 - 而不是 url。

    这是在运行 iOS 5.1 的 iPhone 4 硬件上。

    我输出了 UTF8 字符串,因为我想确保我看到的调试器输出是可信的。

    所以我想我是在说 - 不要在 URL 上使用路径内容,使用一些自制软件或库。

    【讨论】:

    • 双斜杠被简化为单斜杠很可能是由路径方法调用stringByStandardizingPath 造成的,因此不适用于 URL。顺便说一句,我在回复您后立即更新了答案。
    • 这可能是新行为(在过去一年左右) - 我认为带有 '//' 和 '..' 的路径可用于恶意活动(例如,试图转到文件系统中“不同”的地方。
    【解决方案3】:

    怎么样:

    [NSString pathWithComponents:@[@"http://example.com", @"foo", @"bar"]]

    正如 cmets 中所指出的,当使用来自 NSPathUtitlites.h 的方法时,/ 会从协议中剥离,因此这是明显的失败。我能想出的最接近我发布的原始解决方案是:

    [@[ @"http://example.com", @"foo", @"bar" ] componentsJoinedByString:@"/"]
    

    您只需要使用一个文字作为路径分隔符,即NSString does

    NSString 表示路径,一般用“/”作为路径分隔符 和“.”作为扩展分隔符。

    【讨论】:

    • 我从 URL 中剥离了 // :输出是 http:/example.com/foo/bar ,这很糟糕。见下文。
    【解决方案4】:

    stringByAppendingPathComponent 的重点是处理双斜杠,但是,您可以执行以下操作:

    [[@"http://example.com/" stringByAppendingPathComponent:[NSString stringWithFormat:@"%@/%@", @"foo", @"bar"]]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-02
      相关资源
      最近更新 更多