获取URL的不同方式

发布于 2024-11-25 20:54:30 字数 825 浏览 1 评论 0原文

为了获取URL,我通常采用这种方式。

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

当然,urlText 是与 UITextField 关联的。

然而,最近,我在audioStreaming程序中看到了这段代码。
(这是程序。)

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

downloadSourceField 与 UITextField 链接。

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

In order to obtain URL, I usually follow this way.

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

Of coursely, urlText is linked with UITextField.

However, Recently, I saw this code in audioStreaming program.
(This is the program.)

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

downloadSourceField is linked with UITextField.

What is diffence between these two methodes?
When I replaced the second method (escapedValue = ~~~ ) with (escapedValue = downloadSourceField.text;), the program worked well.
Could you let me know what is difference?
And What is the best method to obtain URL for streaming?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凝望流年 2024-12-02 20:54:30

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

The second method will percent-escape some characters which are typically not allowed in URLs. As an example, the space character is not allowed and will be encoded as %20. NSURL does not support passing a string containing a non-allowed character which has not been escaped to +URLWithString:, therefore passing the string through CFURLCreateStringByAddingPercentEscapes first will let you support such URLs.

梦里泪两行 2024-12-02 20:54:30

然而,有趣的

是,深入研究 NSString 文档,您会发现这两个函数:

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

我认为这些是执行此操作的“官方”方式

interesting,

however, digging in the NSString docs you find these two functions:

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

I think those are the "official" way of doing this

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文