2016-02-27 59 views
0

我已經在迅速,我希望從中獲取數據,我tableviewcontroller存儲的XML文件,並查看但是我能似乎找到的方法來獲得從URL這個數據..本地XML文件來填充TableViewController

例如:

let urlString = NSURL(string: 「http://www.blubrry.com/feeds/onorte.xml") 
    let rssUrlRequest:NSURLRequest = NSURLRequest(URL:urlString!) 
    let queue:NSOperationQueue = NSOperationQueue() 

    NSURLConnection.sendAsynchronousRequest(rssUrlRequest, queue: queue) { 
(response, data, error) -> Void in 
self.xmlParser = NSXMLParser(data: data) 
self.xmlParser.delegate = self 
self.xmlParser.parse() 
} 

是否有無論如何我可以簡單地聲明我的XML文件是在哪裏而不是URL?

回答

0

NSURL可用於本地文件以及:

URL objects are the preferred way to refer to local files. Most objects that read data from or write data to a file have methods that accept an NSURL object instead of a pathname as the file reference. For example, you can get the contents of a local file URL as an NSString object by calling the stringWithContentsOfURL:encoding:error: method, or as an NSData object by calling the dataWithContentsOfURL:options:error: method.

+0

會變成這樣,你怎麼辦呢? func beginParsing(){let bundleURL = NSBundle.mainBundle()。bundleURL // let dataFolderURL = bundleURL.URLByAppendingPathComponent(「data」)let fileURL = NSURL(string:ppl.xml,relativeToURL:bundleURL)print(「(Users/hatty/Desktop/ppl.xml)「)parser = NSXMLParser(contentsOfURL:fileURL!)! parser.delegate = self parser.parse()} @dbn – MettyHat

+0

如果您的文件確實在您的包中,那看起來很合適。如果文件不存在,可能要使用if-let來打開可選文件。 – dbn