获取URL的不同方式
为了获取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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
第二种方法将对 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.
然而,有趣的
是,深入研究 NSString 文档,您会发现这两个函数:
我认为这些是执行此操作的“官方”方式
interesting,
however, digging in the NSString docs you find these two functions:
I think those are the "official" way of doing this