Ios Xml解析器委托未在Swift中调用

Ios Xml解析器委托未在Swift中调用,ios,iphone,swift,Ios,Iphone,Swift,我有一个提供xml响应的web服务。我想解析并显示在表中。但未调用我的XMLParser委托。我学斯威夫特比较新。请帮帮我任何人都会被劝服的 class ViewController: UIViewController,NSXMLParserDelegate,UITableViewDelegate { var element:String? var titles:NSMutableString? var link:NSMutableString?

我有一个提供
xml
响应的web服务。我想解析并显示在表中。但未调用我的
XMLParser
委托。我学斯威夫特比较新。请帮帮我任何人都会被劝服的

class ViewController: UIViewController,NSXMLParserDelegate,UITableViewDelegate {
        var element:String?
        var titles:NSMutableString?
        var link:NSMutableString?
        var tableData:NSMutableArray?
        var dict:NSMutableDictionary?
     @IBOutlet weak var table: UITableView?
        override func viewDidLoad() {
            super.viewDidLoad()
            dict = NSMutableDictionary()
            tableData = NSMutableArray()
            let url = NSURL(string: "hp?keytext=s")
            let theRequest = NSURLRequest(URL: url)

            NSURLConnection.sendAsynchronousRequest(theRequest, queue: nil, completionHandler: {(response: NSURLResponse!, data: NSData!, error: NSError!) -> Void in
                if data.length > 0 && error == nil {

                 //var   parser=NSXMLParser(data: data)
               var parser = NSXMLParser(contentsOfURL: url)
                    parser.delegate=self
                    parser.shouldResolveExternalEntities=false
                 parser.parse()
                }
            })
            // Do any additional setup after loading the view.




        }

        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }

        func tableView(tableView: UITableView?, numberOfRowsInSection section: Int) -> Int {

            // Return the number of rows in the section.
            return tableData!.count
        }

        func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {

                  var cell = tableView.dequeueReusableCellWithIdentifier("CELL") as? UITableViewCell


            if !(cell != nil) {
                       cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "CELL")}



            var a:NSString
            var b:String

            a = tableData?.objectAtIndex(indexPath.row).objectForKey("title") as NSString
            b = tableData?.objectAtIndex(indexPath.row).objectForKey("city") as NSString

            cell?.textLabel?.text=a;
             cell?.detailTextLabel?.text=b




            return cell




        }

        func tableView(tableView:UITableView, heightForRowAtIndexPath indexPath:NSIndexPath)->CGFloat

        {

            return 78;

        }


        func parser(parser: NSXMLParser, didStartElement elementName: String, namespaceURI: String, qualifiedName qName: String, attributes attributeDict: [NSObject : AnyObject]) {
            element=elementName
            if element=="restaurant"
            {
                titles=NSMutableString()
                link=NSMutableString()
            }

        }

        func parser(parser: NSXMLParser, foundCharacters string: String) {
            if element=="title"
            {
                titles?.appendString(string)
            }
            else if element=="city"
            {
                link!.appendString(string)
            }
        }


        func parser(parser: NSXMLParser, didEndElement elementName: String, namespaceURI: String, qualifiedName qName: String) {
            if elementName == "restaurant"
            {
                dict?.setValue(titles, forKeyPath: "title")
                dict?.setValue(link, forKeyPath: "city")
                tableData?.addObject(dict!)

            }
        }

        func parserDidEndDocument(parser: NSXMLParser!){

            table?.reloadData()

        }

    }

首先,您的url不是有效的url,您只将此字符串作为url“hp?keytext=s”传递

第二件事是在解析失败时调用这个方法

func parser(parser: NSXMLParser, parseErrorOccurred parseError: NSError) {
        NSLog("failure error: %@", parseError)
    }

您不需要在
viewDidLoad
方法中使用
sendAsynchronousRequest
,而需要使用以下代码:

override func viewDidLoad() {
    super.viewDidLoad()

    let url = NSURL(string: "http://images.apple.com/main/rss/hotnews/hotnews.rss")  //this is example URL

    parser = NSXMLParser(contentsOfURL: url)!
    parser.delegate = self
    parser.parse()

}
在所有函数之外声明
解析器
变量

然后在
viewDidLoad
方法中,用以下代码替换代码:

override func viewDidLoad() {
    super.viewDidLoad()

    let url = NSURL(string: "http://images.apple.com/main/rss/hotnews/hotnews.rss")  //this is example URL

    parser = NSXMLParser(contentsOfURL: url)!
    parser.delegate = self
    parser.parse()

}
您的完整代码将是:

import UIKit

class ViewController: UIViewController,NSXMLParserDelegate,UITableViewDelegate, UITableViewDataSource {    //You will need UITableViewDataSource here.

    var parser : NSXMLParser = NSXMLParser()
    var element:String?
    var titles:NSMutableString?
    var link:NSMutableString?
    var tableData:NSMutableArray?
    var dict:NSMutableDictionary?

    @IBOutlet weak var table: UITableView?

    override func viewDidLoad() {
        super.viewDidLoad()

        let url = NSURL(string: "http://images.apple.com/main/rss/hotnews/hotnews.rss")

        parser = NSXMLParser(contentsOfURL: url)!
        parser.delegate = self
        parser.parse()

    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        // Return the number of rows in the section.
        return tableData!.count
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        var cell = tableView.dequeueReusableCellWithIdentifier("CELL") as? UITableViewCell


        if !(cell != nil) {
            cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "CELL")}
        var a:NSString
        var b:String

        a = tableData?.objectAtIndex(indexPath.row).objectForKey("title") as! NSString
        b = tableData?.objectAtIndex(indexPath.row).objectForKey("city") as! NSString as String

        cell?.textLabel?.text = a as String;
        cell?.detailTextLabel?.text = b

        return cell!

    }

    func tableView(tableView:UITableView, heightForRowAtIndexPath indexPath:NSIndexPath)->CGFloat

    {

        return 78;

    }

    func parser(parser: NSXMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [NSObject : AnyObject]) {
        element=elementName
        if element=="restaurant"
        {
            titles=NSMutableString()
            link=NSMutableString()
        }

    }

    func parser(parser: NSXMLParser, foundCharacters string: String?) {
        if element=="title"
        {
            titles?.appendString(string!)
        }
        else if element=="city"
        {
            link!.appendString(string!)
        }
    }


    func parser(parser: NSXMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {
        if elementName == "restaurant"
        {
            dict?.setValue(titles, forKeyPath: "title")
            dict?.setValue(link, forKeyPath: "city")
            tableData?.addObject(dict!)

        }
    }

    func parserDidEndDocument(parser: NSXMLParser){

        table?.reloadData()

    }

}
我也更新了你的委托函数

并检查您是否从
URL
获取数据

我建议你们先学习教程,这将有助于你们理解一切