string - SWIFT:为什么 "NSURL(string:"返回 Nil,即使它在浏览器中是有效的 url?

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

有什么建议吗?

最佳答案

您应该像这样对 URL 进行 URL 编码:

selectedFeedUrl = selectedFeedUrl.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)

关于string - SWIFT:为什么 "NSURL(string:"返回 Nil,即使它在浏览器中是有效的 url?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28304736/

相关文章:

c - 如何在 C 上将 argv[] 输入分离为两个不同的字符串?

python - 以比 n^2 更低的时间复杂度标记相似的句子

django HttpResponseRedirect 和 ABSOLUTE_URL_OVERRIDES

php - 重定向到主页(索引)

swift - 如何在 Swift 3 中向图像添加文本?

php - 将图像从swift上传到php服务器时出错

php - LEFT JOINING 两个在字符串条件下不互相引用的表是一个坏主意吗?

c++ - 如何声明 wchar_t 并稍后设置其字符串值?

java - 一个在 Java 中进行 URL 查询字符串操作的好库

ios - 将重新排序图标设置为透明 UITableViewCells - moverowat 函数