【问题标题】:Change NSURL string by this method stringByAddingPercentEncodingWithAllowedCharacters通过此方法更改 NSURL 字符串 stringByAddingPercentEncodingWithAllowedCharacters
【发布时间】:2015-09-07 07:41:17
【问题描述】:

我有一个字符串

https://www.example.com/abc/Gesture-abc-mobile-app_%257E0177cae9d41f573e5f?source=rss

当我使用此方法将其转换为 Url 以在 UIWebView 中加载时

 NSString *str = [[NSString stringWithFormat:@"%@", self.urlStr] stringByAddingPercentEncodingWithAllowedCharacters:NSCharacterSet.URLQueryAllowedCharacterSet];

输出是:

 https://www.example.com/jobs/Gesture-initiated-mobile-app_%257E0177cae9d41f573e5f?source=rss%0A%20%20%20%20%20%20

告诉我为什么会这样……

【问题讨论】:

  • 垃圾进,垃圾出?

标签: ios objective-c nsstring nsurl


【解决方案1】:

字符串末尾有多余的字符——确切地说是回车和空格:

NSString *urlStr = @"https://www.example.com/abc/Gesture-abc-mobile-app_%257E0177cae9d41f573e5f?source=rss\n     ";

结果

https://www.example.com/abc/Gesture-abc-mobile-app_%25257E0177cae9d41f573e5f?source=rss%0A%20%20%20%20%20

删除最后的垃圾,你会得到想要的结果。

【讨论】:

  • 谢谢,我怎样才能删除垃圾?
  • <__nsarraym>( { link = "example.com/jobs/…\n "; title = "创建一个简单的 Android 移动应用 - Upwork\n "; }, { link = "example.com/jobs/…\n "; title = "Native IOS & Android App Project - Upwork\n "; },
  • 这甚至不是同一个字符串。
  • link = "https://www.example.com/jobs/Need-iOS-Android-apps-developer_%7E01f48b2d957183b068?source=rss\n ";
猜你喜欢
  • 2020-07-19
  • 1970-01-01
  • 1970-01-01
  • 2023-03-30
  • 2014-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多