SWIFT:为什么" NSURL(字符串:"与Nil一起返回,即使它是浏览器中的有效网址?

时间:2015-02-03 17:08:20

标签: string url swift nsurl null

前两个示例链接工作,第三个返回NIL。

为什么NSUrl为这样的字符串返回nil,即使它在浏览器中是有效的URL?

我是否应该更多地处理字符串?

这是我的代码:

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
    }
  }
}

有什么建议吗?

4 个答案:

答案 0 :(得分:8)

您应该对URL进行URL编码,如下所示:

selectedFeedUrl = selectedFeedUrl.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)

答案 1 :(得分:5)

在iOs 9中:

myString = myString.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())!

希望有所帮助

答案 2 :(得分:4)

对于let url = URL(string:url.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed)!)! ,您可以这样做

{{1}}

答案 3 :(得分:3)

谢谢你们的帮助。 我在代码中添加了这两行,现在可以使用了:

    selectedFeedURL = selectedFeedURL.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!
    selectedFeedURL =  selectedFeedURL.stringByReplacingOccurrencesOfString("%09%09", withString:"")