2016-02-27 55 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