NSURL 返回 null,stringByAddingPercent...返回一场噩梦

发布于 2024-11-25 07:11:16 字数 981 浏览 1 评论 0原文

我会尽量快点,我还很新,但我通常可以解决问题,这个问题让我发疯。我在网站上有一个 XML,将其解析为表格,单击该项目,将链接加载到 webView 中,然后开始问题...

    -(void)viewDidLoad {
NSString *str = [NSString stringWithFormat:@"%@", [item objectForKey:@"link"]];
NSURL *url = [NSURL URLWithString = str];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest = request];
}

如果您 NSLog *str,那就很好。如果您使用 NSLog URL,则它为空。问题显然是使用 objectForKey,因为否则其他一切都有效。现在,如果我

NSURL *url = [NSURL URLWithString = [str stringByAddingPercentEscapesUsingEncoding:...

添加 NSUTF 或 NSASCII,URL 的 NSLog 将添加

%0A%20%20%20%20%20%20%20%20%0A%20%20%0A%20%20

到我的值的末尾。因此,如果该值为 aq1sw2de3,则 nslog 将是

aq1sw2de3%0A%20%20 etc.

我刚刚在 Mac 屏幕上输入的内容,假设代码中不存在所有拼写错误。我尝试了很多不同的方法来将此 URL 传递到 webView,但我的结果要么是 NULL,要么是该死的乱码。我是否在浪费时间做我做不到的事情?我的处理方式是错误的吗?我已经搜索了几个小时,我看到的所有问题都是由于它们传递的字符串或 url 的格式(空格和 | 导致错误)造成的,但我的链接值只是 A 到 Z 和 0 到 9,仅此而已。

I'll try to be quick, I'm still pretty new but I can usually figure stuff out, this issue is driving me crazy. I have an XML on a web site, parse it into a table, click the item, load a link into a webView, and starts the issue...

    -(void)viewDidLoad {
NSString *str = [NSString stringWithFormat:@"%@", [item objectForKey:@"link"]];
NSURL *url = [NSURL URLWithString = str];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest = request];
}

If you NSLog *str, it's good. If you NSLog URL ,it's null. The issue is obviously using an objectForKey, because otherwise everything else works. Now if I

NSURL *url = [NSURL URLWithString = [str stringByAddingPercentEscapesUsingEncoding:...

adding either NSUTF or NSASCII, the NSLog for URL adds

%0A%20%20%20%20%20%20%20%20%0A%20%20%0A%20%20

to end end of my value. So if the value was aq1sw2de3, the nslog would be

aq1sw2de3%0A%20%20 etc.

I just typed this up from lookin at the mac screen, assume all spelling errors don't exist in the code. I've tried so many different way of passing this URL to the webView, but my results are either NULL or all that damn gibberish. Am I wasting my time doing something I can't do? Am I going about it the wrong way? I've been searching for hours, and all the issues I see are due to formatting of the string or url they are passing ( spaces and | causing errors) but my link values are just A through Z and 0 through 9, that's it.

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

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

发布评论

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

评论(3

睡美人的小仙女 2024-12-02 07:11:16

“%20”和“%0A”分别是空格和制表符的百分比转义序列。检查您的字符串末尾是否有一些额外的空格。

"%20" and "%0A" are the percent escape sequences for a space and a tab character, respectively. Check whether your string has some extra whitespace at the end.

追我者格杀勿论 2024-12-02 07:11:16

不知何故,你的字符串正在获取一些额外的空白。如果您只是需要修剪它,请尝试:

NSURL *url = [NSURL URLWithString:
             [[str stringByAddingPercentEscapesUsingEncoding: NSUTF8Encoding]        
             stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];

Somehow, your string is getting picking up some extra whitespace. If you simply need to trim it, try:

NSURL *url = [NSURL URLWithString:
             [[str stringByAddingPercentEscapesUsingEncoding: NSUTF8Encoding]        
             stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
甜柠檬 2024-12-02 07:11:16

斯科特是对的。查看 NSCharacterSet 以获取可用于修剪字符串的一组常量空白字符。您可以使用 NSString 调用 stringByTrimmingCharactersInSet 并设置空白来删除不需要的字符。

Scott's right. Look at the NSCharacterSet for a constant set of whitespace characters you can use to trim your string with. You can use the NSString call stringByTrimmingCharactersInSet with the whitespace set to remove unwanted characters.

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