SWIFT:为什么“NSURL(string:") 返回 Nil,即使它是浏览器中的有效 url? [英] SWIFT: Why is "NSURL(string:" returning with Nil, even though it's a valid url in a browser?

查看:36
本文介绍了SWIFT:为什么“NSURL(string:") 返回 Nil,即使它是浏览器中的有效 url?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

前两个示例链接有效,第三个返回 NIL.

The first two example links are working the third one returns NIL.

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

Why is NSUrl returning nil for such string, even though it's a valid url in a browser?

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

Am I supposed to process the string more?

这是我的代码:

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 编码:

You should URL-encode the URL like this:

selectedFeedUrl = selectedFeedUrl.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)

这篇关于SWIFT:为什么“NSURL(string:") 返回 Nil,即使它是浏览器中的有效 url?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆