2015-02-03 7 views
5

Pierwsze dwa przykładowe linki pracują trzeci zwraca NILSWIFT: Dlaczego „bibliotece NSURL (string:” powrocie z Nil, chociaż jest to prawidłowy adres URL w przeglądarce

Dlaczego bibliotece NSURL powrocie nil za?. ? taki ciąg, choć jest to prawidłowy adres URL w przeglądarce

Czy mam przetworzyć ciąg bardziej

Oto mój kod:?

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

Wszelkie sugestie

+0

Czy jesteś pewien, że wraca zero? Wydaje mi się, że działa dobrze dla mnie. – bjtitus

+0

Może się to również zdarzyć, gdy w adresie URL znajdują się znaki bez znaków specjalnych, w tym końcowa spacja ... – gone

Odpowiedz

8

Należy URL zakodować URL tak:

selectedFeedUrl = selectedFeedUrl.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)

+0

Korzystając z tej metody otrzymuję to: http://www.multirotorusa.com/dji-phantom-dont-be -annoying /% 09% 09, ale strony nie można znaleźć. Dlaczego:% 09% 09 na końcu łańcucha i jak mogę się go pozbyć? –

+0

Przyciąć odstępy od końców łańcucha. % 09 to tylko karta (\ t). – fred02138

3

Dziękuję wam za pomoc. Dodałem te dwie linie do mojego kodu i działa teraz:

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

w iOS 9:

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

nadzieję, że pomoże

+0

Wielkie dzięki, bracie. – Jerome

3

Dla swift3 można zrobić tak

let url = URL(string:url.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed)!)! 
Powiązane problemy