String 斯威夫特:为什么是;NSURL(字符串:“返回时为零,即使它在浏览器中是有效的url?”?
前两个示例链接正在工作,第三个链接返回NIL 为什么NSUrl对这样的字符串返回nil,即使它在浏览器中是有效的url 我应该更多地处理字符串吗 这是我的密码:String 斯威夫特:为什么是;NSURL(字符串:“返回时为零,即使它在浏览器中是有效的url?”?,string,url,swift,nsurl,null,String,Url,Swift,Nsurl,Null,前两个示例链接正在工作,第三个链接返回NIL 为什么NSUrl对这样的字符串返回nil,即使它在浏览器中是有效的url 我应该更多地处理字符串吗 这是我的密码: import UIKit import Foundation class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, NSXMLParserDelegate { var myFeed : NSArray = [] var
import UIKit
import Foundation
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, NSXMLParserDelegate {
var myFeed : NSArray = []
var url : NSURL!
var feedURL : NSURL!
var selectedFeedURL = String()
@IBOutlet var tableFeeds: UITableView!
@IBOutlet var webView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
// Set feed url.
//url = NSURL(string: "http://www.skysports.com/rss/0,20514,11661,00.xml")! //This seems to work
//url = NSURL(string: "http://www.formula1.com/rss/news/latest.rss")! //This seems to work
url = NSURL(string: "http://www.multirotorusa.com/feed/")!
loadRss(url);
}
func loadRss(data: NSURL) {
var myParser : XmlParserManager = XmlParserManager.alloc().initWithURL(data) as XmlParserManager
myFeed = myParser.feeds
tableFeeds.reloadData()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return myFeed.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
var dict : NSDictionary! = myFeed.objectAtIndex(indexPath.row) as NSDictionary
cell.textLabel?.text = myFeed.objectAtIndex(indexPath.row).objectForKey("title") as? String
cell.detailTextLabel?.text = myFeed.objectAtIndex(indexPath.row).objectForKey("description") as? String
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
var indexPath: NSIndexPath = self.tableFeeds.indexPathForSelectedRow()!
var selectedFeedURL = myFeed.objectAtIndex(indexPath.row).objectForKey("link") as String
selectedFeedURL = selectedFeedURL.stringByReplacingOccurrencesOfString(" ", withString:"")
selectedFeedURL = selectedFeedURL.stringByReplacingOccurrencesOfString("\n", withString:"")
// feedURL = NSURL(fileURLWithPath: selectedFeedURL) //This returns with: URL + /%09%09 -- file:///
feedURL = NSURL(string: selectedFeedURL) //This returns with NIL
println("Selected Feed URL: \(selectedFeedURL)")
println("Feed URL: \(feedURL)")
if feedURL != nil {
let request : NSURLRequest = NSURLRequest(URL: feedURL!)
webView.loadRequest(request)
println("Feed URL: \(feedURL)") //Doesn't make it here
}
}
}
有什么建议吗?您应该对URL进行如下URL编码:
selectedFeedUrl=selectedFeedUrl.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)
谢谢大家的帮助。
我将这两行代码添加到我的代码中,现在可以使用了:
selectedFeedURL = selectedFeedURL.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!
selectedFeedURL = selectedFeedURL.stringByReplacingOccurrencesOfString("%09%09", withString:"")
在iOs 9中:
myString = myString.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())!
希望它能对你这样做有帮助
let url = URL(string:url.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed)!)!
你确定它返回的是nil吗?对我来说似乎很好。当URL中有非转义字符时,也可能出现这种情况,包括尾随空格…使用你的方法,我得到了这样的结果:但网页找不到。为什么:%09%09在字符串的末尾,我如何才能去掉它?请从结尾处修剪空格,然后单击对于字符串。%09只是一个制表符(\t)。非常感谢,兄弟。