【问题标题】:The different way to obtain URL获取 URL 的不同方式
【发布时间】:2011-10-11 04:41:00
【问题描述】:

为了获取URL,我一般都是这样的。

    NSString *userText = urlText.text;
    NSURL *url = [NSURL URLWithString:userText];

当然,urlText 与 UITextField 相关联。

然而,最近,我在 audioStreaming 程序中看到了这段代码。
(This is the program.)

NSString *escapedValue =
    [(NSString *)CFURLCreateStringByAddingPercentEscapes(nil, (CFStringRef)downloadSourceField.text, NULL, NULL, 
                                                         kCFStringEncodingUTF8) autorelease];
NSURL *url = [NSURL URLWithString:escapedValue];

downloadSourceField 与 UITextField 链接。

这两种方法有什么区别?
当我用 (escapedValue = downloadSourceField.text;) 替换第二种方法 (escapedValue = ~ ~ ) 时,程序运行良好。 你能告诉我有什么区别吗? 流媒体获取 URL 的最佳方法是什么?

【问题讨论】:

    标签: iphone objective-c url nsurl


    【解决方案1】:

    第二种方法将百分比转义一些通常不允许在 URL 中使用的字符。例如,空格字符是不允许的,它将被编码为 %20。 NSURL 不支持传递包含未转义到 +URLWithString: 的不允许字符的字符串,因此首先通过 CFURLCreateStringByAddingPercentEscapes 传递字符串将使您支持此类 URL。

    【讨论】:

      【解决方案2】:

      有趣,

      然而,在 NSString 文档中挖掘你会发现这两个函数:

      - (NSString *)stringByReplacingPercentEscapesUsingEncoding:(NSStringEncoding)encoding
      - (NSString *)stringByAddingPercentEscapesUsingEncoding:(NSStringEncoding)encoding
      

      我认为这些是这样做的“官方”方式

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-05-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-07
        • 1970-01-01
        • 2014-12-30
        相关资源
        最近更新 更多