2015-02-03 16 views
5

Hai link ví dụ đầu tiên được làm việc với một phần ba trả về NILSWIFT: Tại sao là "NSURL (string:" trở về với Nil, mặc dù đó là một url hợp lệ trong một trình duyệt

Tại sao NSUrl trở về con số không cho?. ? chuỗi như vậy, mặc dù đó là một url hợp lệ trong một trình duyệt

tôi phải xử lý chuỗi hơn

đây là mã của tôi:?

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

Mọi góp ý

+0

Bạn có chắc là nó quay lại không? Dường như làm việc tốt cho tôi. – bjtitus

+0

Điều này cũng có thể xảy ra khi có các ký tự không thoát trong URL, bao gồm, dấu cách ... – gone

Trả lời

8

Bạn nên URL mã hóa URL như sau:

selectedFeedUrl = selectedFeedUrl.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)

+0

Sử dụng phương pháp của bạn Tôi nhận được thông báo này: http://www.multirotorusa.com/dji-phantom-dont-be -nội dung /% 09% 09 nhưng không thể tìm thấy trang web. Tại sao:% 09% 09 ở cuối chuỗi và làm thế nào tôi có thể loại bỏ nó? –

+0

Cắt khoảng trống từ các đầu của chuỗi. % 09 chỉ là một tab (\ t). – fred02138

3

Thank you guys để giúp đỡ của bạn. tôi bổ sung thêm hai dòng sau vào mã của tôi và nó hoạt động bây giờ:

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

Trong iOs 9:

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

Hy vọng nó giúp

+0

Cảm ơn rất nhiều, bro. – Jerome

3

Đối swift3 bạn có thể làm như thế này

let url = URL(string:url.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed)!)! 
Các vấn đề liên quan